Skip to content

Instantly share code, notes, and snippets.

@jgarzik
Created May 15, 2012 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgarzik/2702335 to your computer and use it in GitHub Desktop.
Save jgarzik/2702335 to your computer and use it in GitHub Desktop.
Boost mutex static initialization example
/*
[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