Sign in to follow this  

threads and semaphores

This topic is 3297 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Semaphores are for keeping two threads from accessing the same data at the same time. Suppose I have a main function with a bool value and I pass it to a thread so both the main thread and the new thread both have access to that bool. The spawned thread can change that bool from false to true. Suppose I want my main thread to display something when that bool is changed to true. Is it necessary to put semaphores around wherever the threads access the bool? The main thread will never alter the bool, it only reads it. Is it risky that the main thread will be reading the bool while the spawned thread is in the middle of changing it from false to true? Is that even possible? Does it matter?

Share this post


Link to post
Share on other sites
It sounds like you need to read about condition variables. Now, I'm not aware of any architecture where reading or writing a bool is not atomic, but depending on how you wait for the bool to become true, you may end up wasting a lot of processor time.

Share this post


Link to post
Share on other sites

This topic is 3297 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this