#include <boost/thread.hpp>
#include <iostream>

int ei_teller(0);
boost::mutex m;
boost::condition_variable c;

void thread1() {
	while (1) {
		boost::unique_lock<boost::mutex> lock(m);
		// use lambda function as predicate for wait
		// wait for 5 seconds at most
		if (c.timed_wait(lock, boost::posix_time::seconds(5), []() {
			return ei_teller>=12;
		})) {
			ei_teller-=12;
			std::cout<<"Ei_teller is verlaagd en is nu "<<ei_teller<<std::endl;
		}
		else
			std::cout<<"Schiet eens op!"<<std::endl;
	}
}

void thread2() {
	int n=0;
	std::cout<<"Geef waarden voor n (negatief om te stoppen): ";
	while (n>=0) {
		std::cin>>n;
		if (n>=0) {
			boost::lock_guard<boost::mutex> lock(m);
			ei_teller+=n;
			c.notify_one();
		}
	}
}

int main() { 
	boost::thread t1(&thread1); 
	boost::thread t2(&thread2); 
	t2.join();
	t1.interrupt();
	std::cout<<"Einde."<<std::endl;
	std::cin.get();	std::cin.get();
	return 0; 
}

