#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
//#include <boost/bind.hpp>
#include <boost/thread/tss.hpp>
#include <boost/thread/once.hpp>
#include <boost/thread/condition.hpp>
#include <iostream>
//boost::mutex io_mutex;
//
//struct count
//{
// count(int id):id(id){}
// void operator()()
// {
// for(int i = 0; i < 10; ++i)
// {
// boost::mutex::scoped_lock lock(io_mutex);
// std::cout << id << ": " << i << std::endl;
// }
// }
// int id;
//};
//int main(int argc, char* argv[])
//{
// boost::thread thrd1(count(1));
// boost::thread thrd2(count(2));
//
// thrd1.join();
// thrd2.join();
// return 0;
//}
//boost::mutex io_mutex;
//
//void count(int id)
//{
// for(int i = 0; i < 10; i++)
// {
// boost::mutex::scoped_lock lock(io_mutex);
// std::cout << id << ": " << i << std::endl;
// }
//}
//
//int main(int argc, char* argv[])
//{
// boost::thread thrd1(boost::bind(&count, 1));
// boost::thread thrd2(boost::bind(&count, 2));
// thrd1.join();
// thrd2.join();
// return 0;
//}
//const int BUF_SIZE = 10;
//const int ITERS = 100;
//
//boost::mutex io_mutex;
//
//class buffer
//{
//public:
// typedef boost::mutex::scoped_lock scoped_lock;
//
// buffer():p(0), c(0), full(0)
// {
// }
//
// void put(int m)
// {
// scoped_lock lock(mutex);
// if(full == BUF_SIZE)
// {
// {
// boost::mutex::scoped_lock lock(io_mutex);
// std::cout << "Buffer is full, waiting..." << std::endl;
// }
// while(full == BUF_SIZE)
// cond.wait(lock);
//
// }
// buf[p] = m;
// p = (p+1) % BUF_SIZE;
// ++full;
// cond.notify_one();
// }
//
// int get()
// {
// scoped_lock lk(mutex);
// if(full == 0)
// {
// {
// boost::mutex::scoped_lock lock(io_mutex);
// std::cout << "Buffer is empty, waiting............" << std::endl;
// }
// while(full == 0)
// cond.wait(lk);
// }
// int i = buf[c];
// c = (c+1) % BUF_SIZE;
// --full;
// cond.notify_one();
// return i;
//
// }
//private:
// boost::mutex mutex;
// boost::condition cond;
// unsigned int p, c, full;
// int buf[BUF_SIZE];
//};
//
//buffer buf;
//void writer()
//{
// for(int n = 0; n < ITERS; ++n)
// {
// {
// boost::mutex::scoped_lock lock(io_mutex);
// std::cout << "sending: " << n << std::endl;
// }
// buf.put(n);
// }
//}
//
//void reader()
//{
// for(int x = 0; x < ITERS; ++x)
// {
// int n = buf.get();
// {
// boost::mutex::scoped_lock lock(io_mutex);
// std::cout << "received: " << n << std::endl;
// }
// }
//}
//
//int main(int argc, char* argv[])
//{
// boost::thread thrd1(&reader);
// boost::thread thrd2(&writer);
// thrd1.join();
// thrd2.join();
// return 0;
//}
//boost::mutex io_mutex;
//boost::thread_specific_ptr<int> ptr;
//
//struct count
//{
// count(int id):id(id){}
// void operator()()
// {
// if(ptr.get() == 0)
// ptr.reset(new int(0));
//
// for(int i = 0; i < 10; ++i)
// {
// (*ptr)++;
// boost::mutex::scoped_lock loc(io_mutex);
// std::cout << id << ": " << *ptr << std::endl;
// }
// }
// int id;
//};
//
//int main(int argc, char* argv[])
//{
// boost::thread thrd1(count(1));
// boost::thread thrd2(count(2));
// thrd1.join();
// thrd2.join();
// return 0;
//}
int i = 0;
boost::once_flag flag = BOOST_ONCE_INIT;
void init()
{
++i;
}
void thread()
{
boost::call_once(&init, flag);
}
int main(int argc, char* argv[])
{
boost::thread thrd1(&thread);
boost::thread thrd2(&thread);
thrd1.join();
thrd2.join();
std::cout<< i << std::endl;
return 0;
}