View previous topic :: View next topic |
Author |
Message |
chatgris Guru
Joined: 14 Oct 2002 Posts: 383 Location: Canada
|
Posted: Mon Apr 28, 2003 6:07 am Post subject: Is the ++ family of operators threadsafe? |
|
|
I've been doing a lot of reading lately and I vaguely remember someone saying that the ++ operator wasn't threadsafe in vanilla C because it used a static buffer... A few questions unanswered in my mind are..
1) Is there any truth to this at all?
2) If there is truth to this, does it only apply to multiple threads accessing a single global variable or does it apply to automatic variables in functions that can only ever be accessed by one thread?
If any gurus out there know the answer to this I'd love to know..
Thanks! _________________ Open your mind. Open your source.
Due credit for avatar from http://www.aikida.net |
|
Back to top |
|
|
rac Bodhisattva
Joined: 30 May 2002 Posts: 6553 Location: Japanifornia
|
Posted: Mon Apr 28, 2003 7:09 am Post subject: |
|
|
If you're referring to "all increments sharing a single variable, so incrementing variable A in thread A can munch on incrementing variable B in thread B", I would say that it might be conceivable that a compiler might be so broken, but I would be very surprised to actually encounter one.
If you have multiple threads of control modifying a single variable, using whatever operator or syntax, you as the programmer are responsible for synchronizing them. This is one good argument against global variables. With automatic variables, you are fine. Even if the same function is running twice in two separate threads, each will have their own variable copy. _________________ For every higher wall, there is a taller ladder |
|
Back to top |
|
|
zhenlin Veteran
Joined: 09 Nov 2002 Posts: 1361
|
Posted: Mon Apr 28, 2003 8:08 am Post subject: |
|
|
Isn't an operation like ++ atomic? I know it involves at least three steps, but, still, it's just a matter of automagic locking. |
|
Back to top |
|
|
chatgris Guru
Joined: 14 Oct 2002 Posts: 383 Location: Canada
|
Posted: Mon Apr 28, 2003 8:12 am Post subject: |
|
|
Actually, I'm not so worried about global variables (I lock all my variables with mutex's anyways or use thread specific data areas)
I was jsut wondering if using the ++ operator used a static buffer like strtok (not strtok_r) does or anything like that.. but from the response it seems that my brain has just mangled a lot of information into one incoherent mess.
Thanks! _________________ Open your mind. Open your source.
Due credit for avatar from http://www.aikida.net |
|
Back to top |
|
|
pilla Bodhisattva
Joined: 07 Aug 2002 Posts: 7729 Location: Underworld
|
Posted: Mon Apr 28, 2003 2:00 pm Post subject: |
|
|
I don't think so. It would be too much expensive to implement it even when you don't need it. x = x + 3; is not atomic too. If you want something to be atomic, you will need to protect it with some critical section mechanism, like semaphores or monitors.
zhenlin wrote: | Isn't an operation like ++ atomic? I know it involves at least three steps, but, still, it's just a matter of automagic locking. |
_________________ "I'm just very selective about the reality I choose to accept." -- Calvin |
|
Back to top |
|
|
Esben Apprentice
Joined: 29 Jun 2002 Posts: 244 Location: Copenhagen/Denmark
|
Posted: Mon Apr 28, 2003 4:59 pm Post subject: |
|
|
The (common) macro ATOMIC_INCREMENT would suggest that ++ is not, in general, an atomic operator. Don't count on it... but usually, it goes all right ) _________________ regards, Esben
True trade is honest, but not merciful. Politics is dishonest, no matter how merciful... and war is neither honest nor merciful.... therefore, choose trade above politics, but politics above war. |
|
Back to top |
|
|
|