Quote:
Originally Posted by
Loic Domaigne
Without knowing the problem you are trying to solve, my reply might be off the track.
BUT reading "I have more than 100 variables to synchronize between threads" rings to me bells like "subtle, sporadic bugs, headache debugging and unmaintainable code"... Try to refactor your code to get as less shared variables as possible.
Loïc
The real problem like this,
My program has about 10-15 threads, they do not run at the same time, depending on what service is triggered, and then each thread may maintain 5-6 shared variables; that means, if the thread is enabled then other threads may read/write these variables.
However, each shared variables many be an array or an array of structure, like
struct STRCTU_SAMPLE __variable_name__[ 100 ];
My solution now is to use a critical section, when a thread is read/write these variables, other thread should be wait, until the processing cycle is finished because each thread may read a shared variable from others thread (not just one) and also write result back to other shared variables.
At such case, is critical section better than mutux?
The structure of each thread like this:
PHP Code:
READ shared variables to others.
...
Process
...
//Sub processing
READ shared variables to others.
Process
Write output to share variables.
...
Write output to share variables.
---------- Post updated at 03:45 PM ---------- Previous update was at 03:30 PM ----------
My current solution like this:
Enter critical section
PHP Code:
READ shared variables to others.
...
Process
...
//Sub processing
READ shared variables to others.
Process
Write output to share variables.
...
Write output to share variables.
Leave critical section
Any better solution or structure about such system? Thanks.