Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Is the ++ family of operators threadsafe?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
chatgris
Guru
Guru


Joined: 14 Oct 2002
Posts: 383
Location: Canada

PostPosted: Mon Apr 28, 2003 6:07 am    Post subject: Is the ++ family of operators threadsafe? Reply with quote

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
View user's profile Send private message
rac
Bodhisattva
Bodhisattva


Joined: 30 May 2002
Posts: 6553
Location: Japanifornia

PostPosted: Mon Apr 28, 2003 7:09 am    Post subject: Reply with quote

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
View user's profile Send private message
zhenlin
Veteran
Veteran


Joined: 09 Nov 2002
Posts: 1361

PostPosted: Mon Apr 28, 2003 8:08 am    Post subject: Reply with quote

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
View user's profile Send private message
chatgris
Guru
Guru


Joined: 14 Oct 2002
Posts: 383
Location: Canada

PostPosted: Mon Apr 28, 2003 8:12 am    Post subject: Reply with quote

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
View user's profile Send private message
pilla
Bodhisattva
Bodhisattva


Joined: 07 Aug 2002
Posts: 7729
Location: Underworld

PostPosted: Mon Apr 28, 2003 2:00 pm    Post subject: Reply with quote

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
View user's profile Send private message
Esben
Apprentice
Apprentice


Joined: 29 Jun 2002
Posts: 244
Location: Copenhagen/Denmark

PostPosted: Mon Apr 28, 2003 4:59 pm    Post subject: Reply with quote

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 :o)
_________________
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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
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