how to manage mutithread safety?

in my project of one process,there are 3 threads,one is most important,it’s job is to accept data for hardware,the second is to save data to file when it’s necessary,the third is resposible for managing other job,how to manage them to work well?

I would write them as thread, but as three different processes. Given the broad nature of your question I’l try to give a broad answer ;-) The following idea apply to both threads and processes

Hardware thread would run high priority (how high depends on what it’s doing exaclty) and stuff what ever data it gather in memory ( queue) . The next thread would run low priority and only job would to empty the queue to disk. Data may need to be protected by semaphores/mutexes depending on how the data is organized in memory.

For the third thread then each type of job could get trigger by pulse each having their own priority depending on the type of job.

If it would be processes I would write the hardware part as a driver/resmgr then file buffer would be another resmgr (or just a thread in the third process) and the third one would be the main process taking to all drivers and synchronising everything, the brain if you would.

  • Mario

I make one test,delay time of the first thread(receive data from hardware,activeted by interrupt) is very short,if I write one “printf” in it,the other thread have no chance to receive pulse,why?

What do you mean by have no chance? Do you mean the other thread is not receiving the pulse or that it takes too much time to receive it? What pulse are you talking about, who’s generating it, what priority? Sorry for all these questions but you not giving enough information and your too far away for me to try mind-reading ;-)

If I were to guess what you are asking; it’s normal, if the hardware thread runs the printf at higher priority then the other thread can’t receive the pulse until the hardware thread is not using the CPU, well unless you’d have a SMP system (more then 1 CPU) l-)