Re: [OT]Re: atomic flag

From: Nick Hounsome (nh002_at_blueyonder.co.uk)
Date: 01/15/04


Date: Thu, 15 Jan 2004 06:21:52 -0000


"Jeff Schwab" <jeffplus@comcast.net> wrote in message
news:R7Wdnca8e_HIcpjdRVn-jw@comcast.com...
> Ron Natalie wrote:
> > "Jeff Schwab" <jeffplus@comcast.net> wrote in message
news:27idncgNDfwefZjdRVn-uQ@comcast.com...
> >
> >>Peter Koch Larsen wrote:
> >>
> >>>"Jeff Schwab" <jeffplus@comcast.net> skrev i en meddelelse
> >>>news:886dnfBKB6Y_y5nd4p2dnA@comcast.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.



Relevant Pages

  • Re: [OT]Re: atomic flag
    ... Ron Natalie wrote: ... All the processors on a given machine share some memory. ... Keeping the cache in sync with the memory is done at a much lower level. ...
    (comp.lang.cpp)
  • Re: [Vuln-Dev Challenge] - VulnDev1.c Summary
    ... means that 0x4, 0x5, and 0x8 all do not have the IS_MMAPPED flag set. ... /* size field is or'ed with PREV_INUSE when previous adjacent chunk in use ... > turn calls munmap on an invalid memory address. ... >> glance it may appear that exploitation could occur during free. ...
    (Vuln-Dev)
  • Re: V4FX PPC suspend/resume
    ... Austin's answers seems more robust assuming you clear the "warm boot" flag in step 5 of the resume process. ... copy all of memory to a non-volatile storage ... Copy the saved memory from the non-volatile storage device to ...
    (comp.arch.fpga)
  • Re: [RFC patch] introduce sys_membarrier(): process-wide memory barrier (v9)
    ... This synchronization only takes care of threads using the current process memory ... I am tempted to say that we should probably wait for users of this API feature ... So when userspace start using this flag on old kernels that do not ...
    (Linux-Kernel)
  • Re: [patch 0/9] Critical Mempools
    ... I'm more in favour of a proper reservation system. ... > could reserve memory for a critical process to use, ... >>Well, I've got patches that implement the GFP flag approach, but as I ... networking stack, to keep the network alive under periods of extreme VM ...
    (Linux-Kernel)