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

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

}
