#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <boost/thread.hpp>
#include <iostream>

using namespace boost;
using namespace std;
boost::interprocess::interprocess_semaphore sem(2); //it allows 2 threads to work at the same time at the critical section

void worker(int id) {
	cout << "thread " <<id << "waiting " << endl;
	sem.wait(); //enter the crit sect
	boost::this_thread::sleep_for(boost::chrono::milliseconds(1000));
	cout << "Thread " << id << "Exits " << endl;
	sem.post();
}


int main() {
	boost::thread t1(worker,1);
	boost::thread t2(worker,2);
	boost::thread t3(worker,3);
	boost::thread t4(worker,4);

	t1.join();
	t2.join();
	t3.join();
	t4.join();

	return 0;
}	
