Created
May 15, 2012 14:48
-
-
Save jgarzik/2702335 to your computer and use it in GitHub Desktop.
Boost mutex static initialization example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
[jgarzik@bd tmp]$ g++ -O2 -Wall -g -o mx mx.cc -lboost_thread-mt | |
[jgarzik@bd tmp]$ ./mx | |
1. foo = 42 | |
2. foo = 10 | |
3. foo = 20 | |
*/ | |
#include <cstdio> | |
#include <boost/thread.hpp> | |
static boost::mutex mx; | |
static int foo = 42; | |
static void changeit(int v) | |
{ | |
boost::mutex::scoped_lock scoped_lock(mx); | |
foo = v; | |
} | |
int main (int argc, char *argv[]) | |
{ | |
printf("1. foo = %d\n", foo); | |
changeit(10); | |
printf("2. foo = %d\n", foo); | |
changeit(20); | |
printf("3. foo = %d\n", foo); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment