C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

atomic flag
Goto page 1, 2, 3, 4  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Shayan
Guest





PostPosted: Tue Jan 13, 2004 8:30 pm    Post subject: atomic flag Reply with quote



Is there a boolean flag that can be set atomically without needing to
wrap it in a mutex? This flag will be checked constantly by multiple
threads so I don't really want to deal with the overhead of mutexes or
semaphores. Thanks.

Shayan
Back to top
Jeff Schwab
Guest





PostPosted: Tue Jan 13, 2004 8:34 pm    Post subject: Re: atomic flag Reply with quote



Shayan wrote:
Quote:
Is there a boolean flag that can be set atomically without needing to
wrap it in a mutex? This flag will be checked constantly by multiple
threads so I don't really want to deal with the overhead of mutexes or
semaphores. Thanks.


I believe setting a flag /is/ an atomic action. It's when you want to
bundle multiple operations as an atom that problems may occur


Back to top
Ron Natalie
Guest





PostPosted: Tue Jan 13, 2004 9:08 pm    Post subject: Re: atomic flag Reply with quote




"Shayan" <ssarkar (AT) harris (DOT) com> wrote

Quote:
Is there a boolean flag that can be set atomically without needing to
wrap it in a mutex? This flag will be checked constantly by multiple
threads so I don't really want to deal with the overhead of mutexes or
semaphores. Thanks.

There's no concept of atomicity in general. C has a sig_atomic_t
type, but even that only applies to signal handling.


Back to top
Nick Hounsome
Guest





PostPosted: Tue Jan 13, 2004 10:35 pm    Post subject: Re: atomic flag Reply with quote


"Shayan" <ssarkar (AT) harris (DOT) com> wrote

Quote:
Is there a boolean flag that can be set atomically without needing to
wrap it in a mutex? This flag will be checked constantly by multiple
threads so I don't really want to deal with the overhead of mutexes or
semaphores. Thanks.

Shayan

1. No there isn't.
2. There is no simple mechanism even in assembler that will do this
correctly on a MP system.
3. In my experience waiting on a condition/semaphore can be faster than busy
waiting.




Back to top
Peter Koch Larsen
Guest





PostPosted: Wed Jan 14, 2004 10:28 am    Post subject: [OT]Re: atomic flag Reply with quote


"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> skrev i en meddelelse
news:886dnfBKB6Y_y5nd4p2dnA (AT) comcast (DOT) com...
Quote:
Shayan wrote:
Is there a boolean flag that can be set atomically without needing to
wrap it in a mutex? This flag will be checked constantly by multiple
threads so I don't really want to deal with the overhead of mutexes or
semaphores. Thanks.


I believe setting a flag /is/ an atomic action. It's when you want to
bundle multiple operations as an atom that problems may occur

You are wrong - at least on systems with multiple processors.


/Peter



Back to top
Richard Head
Guest





PostPosted: Wed Jan 14, 2004 11:15 am    Post subject: Re: [OT]Re: atomic flag Reply with quote

On Wed, 14 Jan 2004 11:28:01 +0100, Peter Koch Larsen wrote:

Quote:

"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> skrev i en meddelelse
news:886dnfBKB6Y_y5nd4p2dnA (AT) comcast (DOT) com...
Shayan wrote:
Is there a boolean flag that can be set atomically without needing to
wrap it in a mutex? This flag will be checked constantly by multiple
threads so I don't really want to deal with the overhead of mutexes or
semaphores. Thanks.


I believe setting a flag /is/ an atomic action. It's when you want to
bundle multiple operations as an atom that problems may occur

You are wrong - at least on systems with multiple processors.

/Peter

Not true Oh wise one. Some machine architectures have special
'interlocked' machine instructions of the sort TSTI, INCI, DECI,
etc which are garanteed to use a single bus transaction regardless
of how many processors (i.e. read/write without freeing the bus).
Some other machines use interlocked transactions by default and
others no such instructions. But if the processor can be used in
a multiprocessor then there is defintely some feature that
supports these functions which can be accessed by assembly and
maybe by your compiler.

You will have to examine the machine language for the particular
architecture. If you find such instructions then you need to
test to see if your compiler will generate such instruction or
use the asm directive.


Back to top
Peter Koch Larsen
Guest





PostPosted: Wed Jan 14, 2004 11:36 am    Post subject: Re: [OT]Re: atomic flag Reply with quote


"Richard Head" <rh90p (AT) comcast (DOT) net> skrev i en meddelelse
news:pan.2004.01.14.11.15.07.462006 (AT) comcast (DOT) net...
Quote:
On Wed, 14 Jan 2004 11:28:01 +0100, Peter Koch Larsen wrote:


"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> skrev i en meddelelse
news:886dnfBKB6Y_y5nd4p2dnA (AT) comcast (DOT) com...
Shayan wrote:
Is there a boolean flag that can be set atomically without needing to
wrap it in a mutex? This flag will be checked constantly by multiple
threads so I don't really want to deal with the overhead of mutexes
or
semaphores. Thanks.


I believe setting a flag /is/ an atomic action. It's when you want to
bundle multiple operations as an atom that problems may occur

You are wrong - at least on systems with multiple processors.

/Peter

Not true Oh wise one. Some machine architectures have special
'interlocked' machine instructions of the sort TSTI, INCI, DECI,
etc which are garanteed to use a single bus transaction regardless
of how many processors (i.e. read/write without freeing the bus).
Some other machines use interlocked transactions by default and
others no such instructions. But if the processor can be used in
a multiprocessor then there is defintely some feature that
supports these functions which can be accessed by assembly and
maybe by your compiler.

You will have to examine the machine language for the particular
architecture. If you find such instructions then you need to
test to see if your compiler will generate such instruction or
use the asm directive.

And how do you do that in C++?


/Peter



Back to top
Jeff Schwab
Guest





PostPosted: Thu Jan 15, 2004 12:35 am    Post subject: Re: [OT]Re: atomic flag Reply with quote

Peter Koch Larsen wrote:
Quote:
"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> skrev i en meddelelse
news:886dnfBKB6Y_y5nd4p2dnA (AT) comcast (DOT) com...

Shayan wrote:

Is there a boolean flag that can be set atomically without needing to
wrap it in a mutex? This flag will be checked constantly by multiple
threads so I don't really want to deal with the overhead of mutexes or
semaphores. Thanks.


I believe setting a flag /is/ an atomic action. It's when you want to
bundle multiple operations as an atom that problems may occur


You are wrong - at least on systems with multiple processors.

No, I'm not. The machines I use most have from two to fourteen
processors. All the processors on a given machine share some memory.


Back to top
Ron Natalie
Guest





PostPosted: Thu Jan 15, 2004 12:54 am    Post subject: Re: [OT]Re: atomic flag Reply with quote


"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> wrote

Quote:
Peter Koch Larsen wrote:
"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> skrev i en meddelelse
news:886dnfBKB6Y_y5nd4p2dnA (AT) comcast (DOT) com...

Shayan wrote:

Is there a boolean flag that can be set atomically without needing to
wrap it in a mutex? This flag will be checked constantly by multiple
threads so I don't really want to deal with the overhead of mutexes or
semaphores. Thanks.


I believe setting a flag /is/ an atomic action. It's when you want to
bundle multiple operations as an atom that problems may occur


You are wrong - at least on systems with multiple processors.

No, I'm not. The machines I use most have from two to fourteen
processors. All the processors on a given machine share some memory.


That doesn't say anything about automic operations. More is involved then
just sharing memory. Do these things have cache? It's extremely rare to
see a machine the memory itself is synchronized (the HEP was unique in
this regard).


Back to top
Jeff Schwab
Guest





PostPosted: Thu Jan 15, 2004 1:38 am    Post subject: Re: [OT]Re: atomic flag Reply with quote

Ron Natalie wrote:
Quote:
"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> wrote


Peter Koch Larsen wrote:

"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> skrev i en meddelelse
news:886dnfBKB6Y_y5nd4p2dnA (AT) comcast (DOT) com...


Shayan wrote:


Is there a boolean flag that can be set atomically without needing to
wrap it in a mutex? This flag will be checked constantly by multiple
threads so I don't really want to deal with the overhead of mutexes or
semaphores. Thanks.


I believe setting a flag /is/ an atomic action. It's when you want to
bundle multiple operations as an atom that problems may occur


You are wrong - at least on systems with multiple processors.

No, I'm not. The machines I use most have from two to fourteen
processors. All the processors on a given machine share some memory.



That doesn't say anything about automic operations.

Yes, it does. The memory -- the place where the datum is stored -- is
shared among the processors. A write is atomic.

Quote:
More is involved then
just sharing memory. Do these things have cache? It's extremely rare to
see a machine the memory itself is synchronized (the HEP was unique in
this regard).

Keeping the cache in sync with the memory is done at a much lower level.
It's the concern of the hardware & the OS.


Back to top
Nick Hounsome
Guest





PostPosted: Thu Jan 15, 2004 6:21 am    Post subject: Re: [OT]Re: atomic flag Reply with quote


"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> wrote

Quote:
Ron Natalie wrote:
"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> wrote


Peter Koch Larsen wrote:

"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> skrev i en meddelelse
news:886dnfBKB6Y_y5nd4p2dnA (AT) comcast (DOT) com...


Shayan wrote:


Is there a boolean flag that can be set atomically without needing to
wrap it in a mutex? This flag will be checked constantly by multiple
threads so I don't really want to deal with the overhead of mutexes
or
semaphores. Thanks.


I believe setting a flag /is/ an atomic action. It's when you want to
bundle multiple operations as an atom that problems may occur


You are wrong - at least on systems with multiple processors.

No, I'm not. The machines I use most have from two to fourteen
processors. All the processors on a given machine share some memory.



That doesn't say anything about automic operations.

Yes, it does. The memory -- the place where the datum is stored -- is
shared among the processors. A write is atomic.

More is involved then
just sharing memory. Do these things have cache? It's extremely rare
to
see a machine the memory itself is synchronized (the HEP was unique in
this regard).

Keeping the cache in sync with the memory is done at a much lower level.
It's the concern of the hardware & the OS.


I'm with Ron on this one:
The memory can't be kept totally in sync in the way that I think is being
discussed here -
think about it - it would hammer performance.
The OS helps but only at certain points which means that you have to use
things like mutexes for memory shared between
processors. The locking and unlocking of the mutexes consitute 'memory
barriers'
Locking the mutex throws out cached reads so that you get any new stuff in
the memory.
Unlocking the mutex flushes out cached writes.
Obviously it is possible to have an machine code instruction that does the
whole lot - that is how mutexes in shared memory are written.

Anyway the original poster seemed to want:

volatile bool* flag;

while(flag)
;
flag = true;

This is unlikely to work even on a single processor machine.

Getting back to C++ - I think I read somewhere that there is a clash between
C++ and the POSIX threads decls but
I have never had any problems.



Back to top
Jeff Schwab
Guest





PostPosted: Thu Jan 15, 2004 10:30 am    Post subject: Re: [OT]Re: atomic flag Reply with quote

Nick Hounsome wrote:
Quote:
"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> wrote in message
news:R7Wdnca8e_HIcpjdRVn-jw (AT) comcast (DOT) com...

Ron Natalie wrote:

"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> wrote in message

news:27idncgNDfwefZjdRVn-uQ (AT) comcast (DOT) com...

Peter Koch Larsen wrote:


"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> skrev i en meddelelse
news:886dnfBKB6Y_y5nd4p2dnA (AT) comcast (DOT) com...



Shayan wrote:



Is there a boolean flag that can be set atomically without needing to
wrap it in a mutex? This flag will be checked constantly by multiple
threads so I don't really want to deal with the overhead of mutexes

or

semaphores. Thanks.


I believe setting a flag /is/ an atomic action. It's when you want to
bundle multiple operations as an atom that problems may occur


You are wrong - at least on systems with multiple processors.

No, I'm not. The machines I use most have from two to fourteen
processors. All the processors on a given machine share some memory.



That doesn't say anything about automic operations.

Yes, it does. The memory -- the place where the datum is stored -- is
shared among the processors. A write is atomic.


More is involved then
just sharing memory. Do these things have cache? It's extremely rare

to

see a machine the memory itself is synchronized (the HEP was unique in
this regard).

Keeping the cache in sync with the memory is done at a much lower level.
It's the concern of the hardware & the OS.



I'm with Ron on this one:
The memory can't be kept totally in sync in the way that I think is being
discussed here -
think about it - it would hammer performance.

That's why caching is used.

Quote:
The OS helps but only at certain points which means that you have to use
things like mutexes for memory shared between
processors. The locking and unlocking of the mutexes consitute 'memory
barriers'
Locking the mutex throws out cached reads so that you get any new stuff in
the memory.
Unlocking the mutex flushes out cached writes.
Obviously it is possible to have an machine code instruction that does the
whole lot - that is how mutexes in shared memory are written.

How do you think mutexes work? They rely on the fact that a write is
atomic. Keep in mind that the OP was not asking about a read after
write ("RAW"), only a single write.




Back to top
Peter Koch Larsen
Guest





PostPosted: Thu Jan 15, 2004 4:03 pm    Post subject: Re: [OT]Re: atomic flag Reply with quote


"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> skrev i en meddelelse
news:VeOdnVFMuuZj9pvdRVn-hA (AT) comcast (DOT) com...
Quote:
Nick Hounsome wrote:
"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> wrote in message
news:R7Wdnca8e_HIcpjdRVn-jw (AT) comcast (DOT) com...

Ron Natalie wrote:

"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> wrote in message

news:27idncgNDfwefZjdRVn-uQ (AT) comcast (DOT) com...

Peter Koch Larsen wrote:


"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> skrev i en meddelelse
news:886dnfBKB6Y_y5nd4p2dnA (AT) comcast (DOT) com...



Shayan wrote:



Is there a boolean flag that can be set atomically without needing
to
wrap it in a mutex? This flag will be checked constantly by
multiple
threads so I don't really want to deal with the overhead of mutexes

or

semaphores. Thanks.


I believe setting a flag /is/ an atomic action. It's when you want
to
bundle multiple operations as an atom that problems may occur


You are wrong - at least on systems with multiple processors.

No, I'm not. The machines I use most have from two to fourteen
processors. All the processors on a given machine share some memory.



That doesn't say anything about automic operations.

Yes, it does. The memory -- the place where the datum is stored -- is
shared among the processors. A write is atomic.


More is involved then
just sharing memory. Do these things have cache? It's extremely rare

to

see a machine the memory itself is synchronized (the HEP was unique in
this regard).

Keeping the cache in sync with the memory is done at a much lower level.
It's the concern of the hardware & the OS.



I'm with Ron on this one:
The memory can't be kept totally in sync in the way that I think is
being
discussed here -
think about it - it would hammer performance.

That's why caching is used.

The OS helps but only at certain points which means that you have to use
things like mutexes for memory shared between
processors. The locking and unlocking of the mutexes consitute 'memory
barriers'
Locking the mutex throws out cached reads so that you get any new stuff
in
the memory.
Unlocking the mutex flushes out cached writes.
Obviously it is possible to have an machine code instruction that does
the
whole lot - that is how mutexes in shared memory are written.

How do you think mutexes work? They rely on the fact that a write is
atomic. Keep in mind that the OP was not asking about a read after
write ("RAW"), only a single write.




Mutexes work because they use specialpurpose instructions - on the intel
cpu's using the "lock"-prefix. You can not do that in (portable) C++.

Kind regards
Peter



Back to top
Jeff Schwab
Guest





PostPosted: Thu Jan 15, 2004 5:52 pm    Post subject: Re: [OT]Re: atomic flag Reply with quote

Peter Koch Larsen wrote:
Quote:
"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> skrev i en meddelelse
news:VeOdnVFMuuZj9pvdRVn-hA (AT) comcast (DOT) com...

Nick Hounsome wrote:

"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> wrote in message
news:R7Wdnca8e_HIcpjdRVn-jw (AT) comcast (DOT) com...


Ron Natalie wrote:


"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> wrote in message

news:27idncgNDfwefZjdRVn-uQ (AT) comcast (DOT) com...


Peter Koch Larsen wrote:



"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> skrev i en meddelelse
news:886dnfBKB6Y_y5nd4p2dnA (AT) comcast (DOT) com...




Shayan wrote:




Is there a boolean flag that can be set atomically without needing

to

wrap it in a mutex? This flag will be checked constantly by

multiple

threads so I don't really want to deal with the overhead of mutexes

or


semaphores. Thanks.


I believe setting a flag /is/ an atomic action. It's when you want

to

bundle multiple operations as an atom that problems may occur


You are wrong - at least on systems with multiple processors.

No, I'm not. The machines I use most have from two to fourteen
processors. All the processors on a given machine share some memory.



That doesn't say anything about automic operations.

Yes, it does. The memory -- the place where the datum is stored -- is
shared among the processors. A write is atomic.



More is involved then
just sharing memory. Do these things have cache? It's extremely rare

to


see a machine the memory itself is synchronized (the HEP was unique in
this regard).

Keeping the cache in sync with the memory is done at a much lower level.
It's the concern of the hardware & the OS.



I'm with Ron on this one:
The memory can't be kept totally in sync in the way that I think is

being

discussed here -
think about it - it would hammer performance.

That's why caching is used.


The OS helps but only at certain points which means that you have to use
things like mutexes for memory shared between
processors. The locking and unlocking of the mutexes consitute 'memory
barriers'
Locking the mutex throws out cached reads so that you get any new stuff

in

the memory.
Unlocking the mutex flushes out cached writes.
Obviously it is possible to have an machine code instruction that does

the

whole lot - that is how mutexes in shared memory are written.

How do you think mutexes work? They rely on the fact that a write is
atomic. Keep in mind that the OP was not asking about a read after
write ("RAW"), only a single write.





Mutexes work because they use specialpurpose instructions - on the intel
cpu's using the "lock"-prefix. You can not do that in (portable) C++.

Mutexes can be provided without any such instructions. It might
interest you to look at some of the many-splendored implementations of
the "wait" and "signal" semaphore functions. At any rate, you don't
need a mutex to make a write atomic, unless you're implementing a cache
and are using "write" to mean "input to cache."

And as far as portable C++ goes, a write certainly is atomic, since the
standard provides no support for multiple simultaneous threads of
execution. Beyond that, I don't believe I've ever seen a system where a
write was not an atomic operation. In fact, if thread 1 writes
succesfully to a variable, and thread 2 immediately reads from the same
variable and sees anything other than the value written by thread 1,
then thread 2 is not accessing the same variable. A write is atomic by
definition. If you don't believe so, then we mean different things by
"write."



Back to top
Peter Koch Larsen
Guest





PostPosted: Thu Jan 15, 2004 10:49 pm    Post subject: Re: [OT]Re: atomic flag Reply with quote


"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> skrev i en meddelelse
news:7LCdnZSpk-gYTpvdRVn-ug (AT) comcast (DOT) com...
Quote:
Peter Koch Larsen wrote:
"Jeff Schwab" <jeffplus (AT) comcast (DOT) net> skrev i en meddelelse
news:VeOdnVFMuuZj9pvdRVn-hA (AT) comcast (DOT) com...


[snip]

Quote:


Mutexes work because they use specialpurpose instructions - on the intel
cpu's using the "lock"-prefix. You can not do that in (portable) C++.

Mutexes can be provided without any such instructions. It might
interest you to look at some of the many-splendored implementations of
the "wait" and "signal" semaphore functions. At any rate, you don't
need a mutex to make a write atomic, unless you're implementing a cache
and are using "write" to mean "input to cache."

And as far as portable C++ goes, a write certainly is atomic, since the
standard provides no support for multiple simultaneous threads of
execution.

Standard C++ has no notion of threads (yet), but this does not mean that you
can't call portable C++ code from a multi-threading program.

Quote:
Beyond that, I don't believe I've ever seen a system where a
write was not an atomic operation. In fact, if thread 1 writes
succesfully to a variable, and thread 2 immediately reads from the same
variable and sees anything other than the value written by thread 1,
then thread 2 is not accessing the same variable. A write is atomic by
definition. If you don't believe so, then we mean different things by
"write."


I am sorry, but you are simply plain wrong. But do not take my word for it -
go to comp.programming.threads and study some of the many threads discussing
this very issue.

/Peter



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2, 3, 4  Next
Page 1 of 4

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.