#include <thread>
#include <mutex>
#include <condition_variable>
#include <iostream>
using namespace std;

int ei_teller = 0;
mutex m;
condition_variable c;

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

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

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

