Why does my counter variable not come back to 0 in a multithreaded scenario?

As per request of OP (see comments above) in regards to the updated question/code: The lock statement behaves like a "critical" section i.e. It stops that specific code block from being executed in parallel - the parameter of lock is used as the "syncrhonization object" so that any code block surrounded by a lock on the same variable wouldn't be executed in parallel on a different thread The lock statement doesn't do anything with the variable/its contents - for example it doesn't "freeze" the contents so they can still be change anywhere anytime in any executing code (parallel) as long as that code is not protected by lock and the same variable as a parameter If you look at the link I provided (your points to an outdated documentation version for VS2003) it says explicitely that lock(this) for example is NOT ok to use - the reason and a best practice is described accordingly ALL of the above renders the provided code (updated version) still not thread-safe (although definitively better than the original version).

As per request of OP (see comments above) in regards to the updated question/code: The lock statement behaves like a "critical" section i.e. It stops that specific code block from being executed in parallel - the parameter of lock is used as the "syncrhonization object" so that any code block surrounded by a lock on the same variable wouldn't be executed in parallel on a different thread. The lock statement doesn't do anything with the variable/its contents - for example it doesn't "freeze" the contents so they can still be change anywhere anytime in any executing code (parallel) as long as that code is not protected by lock and the same variable as a parameter.

If you look at the link I provided (your points to an outdated documentation version for VS2003) it says explicitely that lock(this) for example is NOT ok to use - the reason and a best practice is described accordingly... ALL of the above renders the provided code (updated version) still not thread-safe (although definitively better than the original version).

I misunderstood the use of lock, I think I finally got it. Many thanks! – Giuseppe R Oct 3 at 21:09.

Yes, there are situations where a finally block may never run. You can turn off your computer before the finally executes. The code in the try block might never terminate (e.g. Infinite loop).

Environment. FailFast does not run finally blocks. A severe error in the runtime might cause the entire process to crash without executing the finally.

In addition the finally block might start running but not complete if it is interrupted, if it throws an exception, or if it goes into an infinite loop. Here though your problem seems to be that you are using multiple threads but not synchronizing access to shared variables: numberOfRunningThreads++; You need to lock on a shared object here.

OK, I know that! My use case is quite specific and it is not the case of a computer turning off! This is not the answer I'm hoping to get, can you suggest how to rephrase the title?

– Giuseppe R Sep 29 at 13:38 4 @GiuseppeR, based on my guesses of the solution you might rephrase it like "Why does my counter variable not come back to 0 in a multithreaded scenario? ". – Albin Sunnanbo Sep 29 at 13:42.

Your problem is that you don't lock the access of numberOfRunningThreads. Multiple threads can modify this as once causing a race condition where numberOfRunningThreads is not incremented or decremented correctly. You can use Interlocked.

Increment, Interlocked. Decrement instead of ++ and ...

Thanks, I wrongly assumed, since the lock() did not accept an int, that int operations were atomic! :( Do you have any idea why ThreadPool. SetMaxThreads is however allowing the creation of more than 10 threads?

– Giuseppe R Sep 29 at 13:55 3 @GiuseppeR int read and int write is an atomic operation, but i++ is a read operation, a compute operation and a write operation. If I changes during the compute operation things go bananas. – Albin Sunnanbo Sep 29 at 13:57.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions