#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <boost/thread.hpp>
#include <iostream>
boost::interprocess::interprocess_semaphore sem(2); //allows 2 threads

using namespace std;

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();
}
