#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
using namespace boost;
boost::interprocess::interprocess_semaphore sem(2);//allows 2 threads to work simaltenouselly

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

return 0;
}
