/*
 * http://charette.no-ip.com:81/programming/2011-06-27_StdThread/
 * $Id: 2011-07-16_static.cpp 271 2011-07-17 04:57:47Z stephane $
 *
 * Example code for static variables when using std::thread in C++0x.
 *
 * Compiled with the following:
 *
 *		g++ -std=c++0x -pthread 2011-07-16_static.cpp
 */


#include <thread>
#include <iostream>


void fooFunc( void )
{
	// use only one of the following "foo" variables:

	// static variable shared across all threads
	static int foo = 0;

	// static variable that uses thread-local-storage; not shared between threads
	static thread_local int foo = 0;

	// I found that "thread_local" wasn't recognized, though the
	// older "__thread" extension seems to be working fine
	static __thread int foo = 0;

	foo ++;
	std::cout << "Foo is now: " << foo << std::endl;
	return;
}


int main( int argc, char *argv[] )
{
	std::cout << "Main thread:  ";
	fooFunc();

	std::cout << "Thread #1:  ";
	std::thread t = fooFunc;
	t.join();

	std::cout << "Thread #2:  ";
	t = fooFunc;
	t.join();

	std::cout << "Main thread again:  ";
	fooFunc();

	return 0;
}

