all groups > dotnet clr > july 2004 >
You're in the

dotnet clr

group:

bool - thread safety



bool - thread safety rhashemian NO[at]SPAM hotmail.com
7/30/2004 1:17:13 PM
dotnet clr: hi all,
this is what the clr docs say about boolean:

This type is safe for multithreaded operations


does this mean i can access a bool object from any thread
without worrying about sync issues? pretty sure that's it,
but just wanna make 100% sure.

thanks,
Re: bool - thread safety Valery Pryamikov
7/30/2004 11:10:10 PM
Hi,
it only means that reads and writes are atomic. However, there are no
guarantee from rearranging memory access and similar. I.e. if you are using
it independently (in isolation) from the rest of your variables - you will
be 100% safe, but this is kind of ...unrealistic...

-Valery.
http://www.harper.no/valery

[quoted text, click to view]

Re: bool - thread safety Christopher Wells
7/31/2004 12:37:42 PM
For example, it's safe to have code like this:

class C
{
bool m_thread_must_exit = false;
public C()
{
//start a worker thread which runs the thread_procedure
}
void thread_procedure()
{
while (!m_thread_must_exit)
{
//...do thread work...
}
}
public void stop_the_thread()
{
//following operation is safe
//even though this thread is writing to bool m_thread_must_exit
//while the worker thread is reading from bool m_thread_must_exit

//tell the worker thread to exit
m_thread_must_exit = true;
}
}

[quoted text, click to view]

Re: bool - thread safety Christopher Wells
7/31/2004 3:02:28 PM

[quoted text, click to view]

So I need to declare the variable as volatile.

[quoted text, click to view]

FWIW I *have* seen volatile being necessary in release-mode C++ (to prevent
the variable being enregistered).

Re: bool - thread safety Jon Skeet [C# MVP]
7/31/2004 6:16:02 PM
[quoted text, click to view]

No it's not. There's nothing to force the worker thread to "see" the
new value of m_thread_must_exit unless there's a "write" memory barrier
in the writing thread and a "read" memory barrier.

In practice it's unlikely to cause any problems with the current
implementation on current processors, but I always try to code to the
spec rather than to the current implementation.

See http://www.pobox.com/~skeet/csharp/multithreading.html#volatile
for more information.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Re: bool - thread safety Jon Skeet [C# MVP]
7/31/2004 8:09:31 PM
[quoted text, click to view]

Yup, or use a lock (which has implicit memory barriers). Personally I
tend to use locks, but that's just a personal preference - I find it
easier to get things right if I stick to one way of doing things for
everything, and as soon as you need to keep two or three variables
updated "transactionally" you need a lock anyway...

[quoted text, click to view]

Glad I'm not just being far too paranoid :)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Re: bool - thread safety Valery Pryamikov
8/1/2004 9:32:29 AM
Jon already answered this, however I'll just drop my 2c here.
Depending on cycle body you code may fail in release build regardless of
used processor architecture.
For example: read access could be moved out of cycle and that mean that your
cycle will never exit on m_thread_must_exit change.

-Valery.
http://www.harper.no/valery

[quoted text, click to view]

Re: bool - thread safety Christopher Wells
8/1/2004 4:44:03 PM

[quoted text, click to view]

Yeah, volatile is needed.

Still, it *is* an example of where you need thread-safe access to a single
bool, which is used "independently (in isolation) from the rest of your
variables" [I'm assuming in my example that the thread procedure may access
other member variables, that are initialized before the thread is started,
but that the m_thread_must_exit is the only variable that is accessed by two
threads concurrently].

Re: bool - thread safety Valery Pryamikov
8/2/2004 7:35:24 AM
Not exactly,
you was using boolean variable for controlling value of instruction pointer
register that could be considered (and sometimes even called) as program's
metavariable.

-Valery.
http://www.harper.no/valery

[quoted text, click to view]

Re: bool - thread safety rhashemian NO[at]SPAM hotmail.com
8/3/2004 12:26:56 PM
thanks all for the responses.
what i gather from all this is that it's safe to read/write
to a bool from various threads but the read value may not
be fresh, unless used inside a lock() block or declared as volatile.
decided to go with volatite.

btw, i need this bool for a windows service as a flag indicating
when the service is being stopped. at such point it will
indicate to the running function (which runs on a separate
worker thread and continuously checks the flag), to stop gracefully.

regards,
rh

[quoted text, click to view]
AddThis Social Bookmark Button