Should you block resources while reading values?

When performing thread synchronization in C # should I also block the object when I read a value or just change it?

for example, I have a Queue <T> object. Should I just block it while doing Enqueue and Dequeue or should I also block it while checking values ​​like Count?

+3
source share
4 answers

From MSDN:

< (Of < (T > ) > ) , . , - . , . , .

, , (, , ).

, . , , . , ( ):

if(queue.Count > 0)
    queue.Dequeue();
+3

, . /.

/ , , , .

+2

, . , Count, . , , dequeue, , 1 . , ​​, .

+1

The CLR guarantees atomic readout of values ​​up to the width of the processor. Therefore, if you work on 32 bits, reading intwill be atomic. If you are working on a 64-bit machine, reading longwill be atomic. Ergo, if there Countis Int32, no need to block.

This post relates to your question.

0
source

All Articles