Thread safe increment of static local variable
void foo() {
static int id = 0;
const int local_id = id++;
//do something with local_id;
}
Multiple threads can call foo in parallel multiple times. I want each call of foo use "unique" value of local_id. Is it ok with the above code? I wonder if second thread assign the value of id to local_id before the value is increased by the first thread. If it is not safe, is there any standard solution for this?
c++ multithreading static
|
show 1 more comment
void foo() {
static int id = 0;
const int local_id = id++;
//do something with local_id;
}
Multiple threads can call foo in parallel multiple times. I want each call of foo use "unique" value of local_id. Is it ok with the above code? I wonder if second thread assign the value of id to local_id before the value is increased by the first thread. If it is not safe, is there any standard solution for this?
c++ multithreading static
9
usestd::atomic_int
– Mgetz
Oct 28 '14 at 14:27
Note that the initialization of id may or may not be threadsafe depending on whether your compiler supports "magic statics".
– dlf
Oct 28 '14 at 14:30
@dlf Right, but it's worth mentioning that "magic statics" are required by C++11, so it's a question of compiler conformance, not of supporting an optional feature.
– Angew
Oct 28 '14 at 14:38
@Agnew, true, but the question doesn't call out C++11 :). It's too late to edit, but otherwise I'd revise it to say "whether or not you have a C++11 compiler that supports magic statics".
– dlf
Oct 28 '14 at 14:42
@All, Thank you :)
– strugi
Oct 28 '14 at 14:43
|
show 1 more comment
void foo() {
static int id = 0;
const int local_id = id++;
//do something with local_id;
}
Multiple threads can call foo in parallel multiple times. I want each call of foo use "unique" value of local_id. Is it ok with the above code? I wonder if second thread assign the value of id to local_id before the value is increased by the first thread. If it is not safe, is there any standard solution for this?
c++ multithreading static
void foo() {
static int id = 0;
const int local_id = id++;
//do something with local_id;
}
Multiple threads can call foo in parallel multiple times. I want each call of foo use "unique" value of local_id. Is it ok with the above code? I wonder if second thread assign the value of id to local_id before the value is increased by the first thread. If it is not safe, is there any standard solution for this?
c++ multithreading static
c++ multithreading static
edited Oct 28 '14 at 14:49
Wolf
5,32332776
5,32332776
asked Oct 28 '14 at 14:26
strugistrugi
6415
6415
9
usestd::atomic_int
– Mgetz
Oct 28 '14 at 14:27
Note that the initialization of id may or may not be threadsafe depending on whether your compiler supports "magic statics".
– dlf
Oct 28 '14 at 14:30
@dlf Right, but it's worth mentioning that "magic statics" are required by C++11, so it's a question of compiler conformance, not of supporting an optional feature.
– Angew
Oct 28 '14 at 14:38
@Agnew, true, but the question doesn't call out C++11 :). It's too late to edit, but otherwise I'd revise it to say "whether or not you have a C++11 compiler that supports magic statics".
– dlf
Oct 28 '14 at 14:42
@All, Thank you :)
– strugi
Oct 28 '14 at 14:43
|
show 1 more comment
9
usestd::atomic_int
– Mgetz
Oct 28 '14 at 14:27
Note that the initialization of id may or may not be threadsafe depending on whether your compiler supports "magic statics".
– dlf
Oct 28 '14 at 14:30
@dlf Right, but it's worth mentioning that "magic statics" are required by C++11, so it's a question of compiler conformance, not of supporting an optional feature.
– Angew
Oct 28 '14 at 14:38
@Agnew, true, but the question doesn't call out C++11 :). It's too late to edit, but otherwise I'd revise it to say "whether or not you have a C++11 compiler that supports magic statics".
– dlf
Oct 28 '14 at 14:42
@All, Thank you :)
– strugi
Oct 28 '14 at 14:43
9
9
use
std::atomic_int
– Mgetz
Oct 28 '14 at 14:27
use
std::atomic_int
– Mgetz
Oct 28 '14 at 14:27
Note that the initialization of id may or may not be threadsafe depending on whether your compiler supports "magic statics".
– dlf
Oct 28 '14 at 14:30
Note that the initialization of id may or may not be threadsafe depending on whether your compiler supports "magic statics".
– dlf
Oct 28 '14 at 14:30
@dlf Right, but it's worth mentioning that "magic statics" are required by C++11, so it's a question of compiler conformance, not of supporting an optional feature.
– Angew
Oct 28 '14 at 14:38
@dlf Right, but it's worth mentioning that "magic statics" are required by C++11, so it's a question of compiler conformance, not of supporting an optional feature.
– Angew
Oct 28 '14 at 14:38
@Agnew, true, but the question doesn't call out C++11 :). It's too late to edit, but otherwise I'd revise it to say "whether or not you have a C++11 compiler that supports magic statics".
– dlf
Oct 28 '14 at 14:42
@Agnew, true, but the question doesn't call out C++11 :). It's too late to edit, but otherwise I'd revise it to say "whether or not you have a C++11 compiler that supports magic statics".
– dlf
Oct 28 '14 at 14:42
@All, Thank you :)
– strugi
Oct 28 '14 at 14:43
@All, Thank you :)
– strugi
Oct 28 '14 at 14:43
|
show 1 more comment
2 Answers
2
active
oldest
votes
Your code is not thread-safe, because multiple threads can read id
concurrently, and producing the same value of local_id
.
If you want a thread-safe version, use std::atomic_int
, which is available in C++11:
void foo() {
static std::atomic_int id;
const int local_id = id++;
//do something with local_id;
}
3
Note that if you are using a Microsoft compiler, the initialization ofid
will not be threadsafe since they don't yet support "magic statics" (as of VS2013). In other words, iffoo
has never been called and then two threads both call it at the same time,id
's constructor could run twice in parallel with unpredictable results.
– dlf
Oct 28 '14 at 14:55
add a comment |
Your code is not thread safe because two threads may increment id at the same time.
Use mutual exclusion or std::atomic for the shared id variable.
1
How can mutual exclusion be used in the above case? Can you please elaborate a little bit further?
– Wolf
Oct 28 '14 at 14:33
Wrap the static inside a class and provide get/increment methods that acquire the mutex before performing the operation.
– Claudio
Oct 28 '14 at 14:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26610831%2fthread-safe-increment-of-static-local-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your code is not thread-safe, because multiple threads can read id
concurrently, and producing the same value of local_id
.
If you want a thread-safe version, use std::atomic_int
, which is available in C++11:
void foo() {
static std::atomic_int id;
const int local_id = id++;
//do something with local_id;
}
3
Note that if you are using a Microsoft compiler, the initialization ofid
will not be threadsafe since they don't yet support "magic statics" (as of VS2013). In other words, iffoo
has never been called and then two threads both call it at the same time,id
's constructor could run twice in parallel with unpredictable results.
– dlf
Oct 28 '14 at 14:55
add a comment |
Your code is not thread-safe, because multiple threads can read id
concurrently, and producing the same value of local_id
.
If you want a thread-safe version, use std::atomic_int
, which is available in C++11:
void foo() {
static std::atomic_int id;
const int local_id = id++;
//do something with local_id;
}
3
Note that if you are using a Microsoft compiler, the initialization ofid
will not be threadsafe since they don't yet support "magic statics" (as of VS2013). In other words, iffoo
has never been called and then two threads both call it at the same time,id
's constructor could run twice in parallel with unpredictable results.
– dlf
Oct 28 '14 at 14:55
add a comment |
Your code is not thread-safe, because multiple threads can read id
concurrently, and producing the same value of local_id
.
If you want a thread-safe version, use std::atomic_int
, which is available in C++11:
void foo() {
static std::atomic_int id;
const int local_id = id++;
//do something with local_id;
}
Your code is not thread-safe, because multiple threads can read id
concurrently, and producing the same value of local_id
.
If you want a thread-safe version, use std::atomic_int
, which is available in C++11:
void foo() {
static std::atomic_int id;
const int local_id = id++;
//do something with local_id;
}
answered Oct 28 '14 at 14:28
dasblinkenlightdasblinkenlight
613k607801200
613k607801200
3
Note that if you are using a Microsoft compiler, the initialization ofid
will not be threadsafe since they don't yet support "magic statics" (as of VS2013). In other words, iffoo
has never been called and then two threads both call it at the same time,id
's constructor could run twice in parallel with unpredictable results.
– dlf
Oct 28 '14 at 14:55
add a comment |
3
Note that if you are using a Microsoft compiler, the initialization ofid
will not be threadsafe since they don't yet support "magic statics" (as of VS2013). In other words, iffoo
has never been called and then two threads both call it at the same time,id
's constructor could run twice in parallel with unpredictable results.
– dlf
Oct 28 '14 at 14:55
3
3
Note that if you are using a Microsoft compiler, the initialization of
id
will not be threadsafe since they don't yet support "magic statics" (as of VS2013). In other words, if foo
has never been called and then two threads both call it at the same time, id
's constructor could run twice in parallel with unpredictable results.– dlf
Oct 28 '14 at 14:55
Note that if you are using a Microsoft compiler, the initialization of
id
will not be threadsafe since they don't yet support "magic statics" (as of VS2013). In other words, if foo
has never been called and then two threads both call it at the same time, id
's constructor could run twice in parallel with unpredictable results.– dlf
Oct 28 '14 at 14:55
add a comment |
Your code is not thread safe because two threads may increment id at the same time.
Use mutual exclusion or std::atomic for the shared id variable.
1
How can mutual exclusion be used in the above case? Can you please elaborate a little bit further?
– Wolf
Oct 28 '14 at 14:33
Wrap the static inside a class and provide get/increment methods that acquire the mutex before performing the operation.
– Claudio
Oct 28 '14 at 14:50
add a comment |
Your code is not thread safe because two threads may increment id at the same time.
Use mutual exclusion or std::atomic for the shared id variable.
1
How can mutual exclusion be used in the above case? Can you please elaborate a little bit further?
– Wolf
Oct 28 '14 at 14:33
Wrap the static inside a class and provide get/increment methods that acquire the mutex before performing the operation.
– Claudio
Oct 28 '14 at 14:50
add a comment |
Your code is not thread safe because two threads may increment id at the same time.
Use mutual exclusion or std::atomic for the shared id variable.
Your code is not thread safe because two threads may increment id at the same time.
Use mutual exclusion or std::atomic for the shared id variable.
answered Oct 28 '14 at 14:29
ClaudioClaudio
7,55812254
7,55812254
1
How can mutual exclusion be used in the above case? Can you please elaborate a little bit further?
– Wolf
Oct 28 '14 at 14:33
Wrap the static inside a class and provide get/increment methods that acquire the mutex before performing the operation.
– Claudio
Oct 28 '14 at 14:50
add a comment |
1
How can mutual exclusion be used in the above case? Can you please elaborate a little bit further?
– Wolf
Oct 28 '14 at 14:33
Wrap the static inside a class and provide get/increment methods that acquire the mutex before performing the operation.
– Claudio
Oct 28 '14 at 14:50
1
1
How can mutual exclusion be used in the above case? Can you please elaborate a little bit further?
– Wolf
Oct 28 '14 at 14:33
How can mutual exclusion be used in the above case? Can you please elaborate a little bit further?
– Wolf
Oct 28 '14 at 14:33
Wrap the static inside a class and provide get/increment methods that acquire the mutex before performing the operation.
– Claudio
Oct 28 '14 at 14:50
Wrap the static inside a class and provide get/increment methods that acquire the mutex before performing the operation.
– Claudio
Oct 28 '14 at 14:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26610831%2fthread-safe-increment-of-static-local-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
9
use
std::atomic_int
– Mgetz
Oct 28 '14 at 14:27
Note that the initialization of id may or may not be threadsafe depending on whether your compiler supports "magic statics".
– dlf
Oct 28 '14 at 14:30
@dlf Right, but it's worth mentioning that "magic statics" are required by C++11, so it's a question of compiler conformance, not of supporting an optional feature.
– Angew
Oct 28 '14 at 14:38
@Agnew, true, but the question doesn't call out C++11 :). It's too late to edit, but otherwise I'd revise it to say "whether or not you have a C++11 compiler that supports magic statics".
– dlf
Oct 28 '14 at 14:42
@All, Thank you :)
– strugi
Oct 28 '14 at 14:43