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

using namespace std;

boost::interprocess::interprocess_semaphore sem(2); //allows two threads to 
//work in the same critical section

void worker(int id) {
 	cout<<"Thread: "<<id<<" waiting"<<endl;
 	sem.wait(); //enter critical section
 	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();
 }
