#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);
        while (ei_teller < 12)
            c.wait(lock);
        ei_teller -= 12;
        cout << "Ei_teller is verlaagd en is nu " << ei_teller << 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_all();
        }
    }
}   

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