Osama Javed
Osama Javed

Reputation: 1442

multiple thread writing to the same boolean

I am working on a graph ( nodes and vertices ) partitioning algorithm.

I use multiple threads to try and identify certain regions inside the graph.

Once a node has been identified as a part of a region, I set a boolean marked to true for the node object.

Multiple threads can attempt to mark the same node at the same time.

Currently I use synchronization to ensure nothing bad happens.

However since I never read the value of marked till after all the threads have finished processing. Would it be possible for me to get rid of the synchronization code? In other words can anything go wrong when concurrently writing to a boolean variable?

Upvotes: 6

Views: 2956

Answers (5)

electroCutie
electroCutie

Reputation: 194

As others have said there is no risk of corruption or incorrect values to the boolean if you are simply trying to set it to the same value from multiple threads.

However, you may not even need that

I never read the value of marked till after all the threads have finished processing.

You obviously need some sort of barrier to synchronize the coordinating thread with the worker threads (such as Thread.join() or CountdownLatch or your primitive du jour) and nearly all of those already provide a happens-before relationship that will make all your marks visible to the coordinator thread.

Having that single point of synchronization also just happens to be cheaper than reading a large number of volatiles (and I wouldn't call that premature optimization, simply eliding the need for volatiles)

Upvotes: 2

Gray
Gray

Reputation: 116908

can anything go wrong when concurrently writing to a boolean variable?

Yes and no. Certainly the resulting value won't be somehow corrupted however it is going to be non-deterministic on which of the updates gets set on the field and when these updates are seen by other threads -- if at all.

If you have multiple threads making decisions using the value of this boolean, you have to at some point provide memory synchronization. Making the field volatile costs very little and unless you have evidence that it is a performance problem, not having the field be volatile is most likely premature optimization. If you are comparing and setting then an AtomicBoolean is recommended which wraps a volatile boolean and provides higher level methods like compareAndSet(...).

Upvotes: 4

Aleksander Blomskøld
Aleksander Blomskøld

Reputation: 18552

No, nothing could go wrong when multiple threads write to the same boolean value, but there can be a problem of reading the value (even a long time) later in a different thread. You should at least mark the variables as volatile to prevent the problem.

Upvotes: 2

Jakub Zaverka
Jakub Zaverka

Reputation: 8874

In theory, no, but I wouldn't mind declaring the variable volatile. Volatile keyword ensures atomic access.

(Provided that the order of the writes do not matter and all reads occur after all writes.)

Upvotes: 1

Seeta Somagani
Seeta Somagani

Reputation: 787

No. If the order of the writes to that variable do not matter.

Upvotes: 0

Related Questions