I 've the following code:
int counter = 1;
var lockTarget = new object();
Parallel.For(1, totalSIM, i => {
/* do some stuff */
lock(lockTarget) {
_printConcurrent(counter++);
}
});
and I need to call _printConcurrent every ~200 times, not every time. I thought about do a MOD (%) for counter, but I can't understand how to use the condition. I enclosed the lock in an if but I can't use the counter so I'm in a logic loop instead the code :S
Any help will be appreciated.
_printConcurrent is only an output of the process, I sacrifice some performance for monitoring data.
You could increment atomically the
counterwithout locking, with theInterlocked.Incrementmethod, and enter thelockonly if the result of the atomic increment is divisible by 200:This way the
_printConcurrentwill be called once every 200 iterations, without blocking the rest 199 iterations.Update: The above approach has a minor flaw. It doesn't guarantee that the
_printConcurrentwill be called sequentially with an incremented argument. For example it is theoretically possible that the_printConcurrent(400)will be called before the_printConcurrent(200). That's because the operating system can suspend any thread at any time for a duration of around 10-30 milliseconds (demo), so a thread could be suspended immediately after theInterlocked.Incrementline, and lose the race to enter thelockby a non-suspended thread. In case this is a problem, you can solve it be throwing a second counter in the mix, that is incremented only inside thelock: