LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] Test-and-set in C/C++

[ILUG] Test-and-set in C/C++

Paul Jakma paul at clubi.ie
Sat Oct 2 03:22:18 IST 2004


On Fri, 1 Oct 2004, Brian Foster wrote:

> yes.
> you cannot.
> not in _portable_ C anyways.
> there is no way, none, nothing, nil, nana, of writing
> any form of an atomic read-modify-write in de jour
> (ISO) standard conformant portable C (or C++, AFAIK).
>
> this always has to be done with machine-specific code,
> usually assembler, such as the hint/example someone
> posted previously --- which was specific to the x86
> (I think) _and_ GNU gcc(1).  that sort of specificity
> is very common.

Note though that some unix platforms /do/ provide C wrappers to 
atomic operations somewhere: man test_and_set on IRIX, man atomic_ops 
on Solaris and less /usr/include/asm/bitops.h on Linux.

Not portable though.

I'd have to wonder why Vincent wants atomic test and set though...

For signals, just increment a flag integer^Wsig_atomic_t and let 
something poll it, and/or catch the signal some other way (eg EINTR 
from select) - and let that sort things and do the /real/ work, where 
the full thread API is nicely available. Eg, something like:

 	sig_atomic_t globalflag;

sig handler:

 	if (globalflag < -1)
 	  /* over flow, set an overflow flag? */
 	else
 	 globalflag++

is probably sufficient for most purposes (on 2's complement machines, 
which is most common machines, no?). The non-signal-context 
poll/check of flag should be able to tell whether overflow occured, 
and it'll just have to have to deal with that, but unless you expect 
to handle massive amounts of signals (or check globalflag /rarely/), 
it's very unlikely ever to.

regards,
-- 
Paul Jakma	paul at clubi.ie	paul at jakma.org	Key ID: 64A2FF6A
Fortune:
In computing, the mean time to failure keeps getting shorter.



More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell