Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating a single value from multiple threads

I have a counter - let's just call it : ItemsRemaining.

I will have a dynamic amount of threads, but for this example, let's say I have 10 threads.....

each thread will contain a loop, and each iteration in the loop does a unit of work, then should update the ItemsRemaining variable on GUI thread. This could be a private int, or it could even be a control value.

How can I accurately update this ItemsRemaining from multiple thread calls? While at the same time not taking too big a performance knock from locking?

like image 909
JL. Avatar asked Mar 20 '26 01:03

JL.


1 Answers

You should check the methods from the Interlocked class.

like image 102
Florin Dumitrescu Avatar answered Mar 22 '26 14:03

Florin Dumitrescu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!