I want to know if i attempt to set bits inside a char/integer from multiple threads, will i lose any modification? I will have all zeros initially and the threads will only set it to 1.
The system architecture is x64.
Thanks,
Gokul.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
On x64 (and x86) operations on ints are atomic. This means that reads and writes are independently atomic, not in combination. (E.g. If you have two threads write an int at the same time, you won’t see one half of one and one half of the other — you will always see one full int or the other.)
However, reading and writing are two separate operations. You can perform some operations interlocked (causing the read and write to be one atomic operation) but I don’t believe that bitwise logic falls into the set of operations that can be performed in this manner.
I think you will have to lock on some mutex/monitor and perform the read/write if you want this operation to be atomic.