Why Atomic operations are not always thread safe

21 בספטמבר 2008

2 תגובות

My blog has moved. You can view this post at the following address: http://blog.drorhelper.com/2008/09/why-atomic-operations-are-not-always.html

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *

2 תגובות

  1. Brennan Fee10 באוקטובר 2008 ב 1:09

    While your point is very valid that the bool _bar will not be "atomic" inside of an if statement… your fix isn't really a fix.

    Just slapping a lock around the entire IF won't help unless you also lock every time you read or write the bool. Locking only helps if you lock EVERY time you access the protected value. Furthermore, every thread needs to use the SAME lock object to achieve the desired effect.

    That being said, most of the time all you want is to READ a value and then move on. You usually don't care if the value was changed a split second AFTER you read it. In fact, most of the time when locking is used it is used to write to a local variable and then action is taken on the local variable outside the lock condition.

  2. dhelper15 בנובמבר 2008 ב 12:24

    While basically you're write there is no "fool proof" way to create thread-safe code. Bsically it depends on what you're need to do with the specific variable. And if what you need is to put a lock on each access to the variable then that's the way to write.