// File: /workspace/github_cpp_makefile_star2_O2/wellenvogel[P]simple-cpp-threads/test.cpp
// Function: main
int main(int argc, char ** argv){
	cout << "hello world" << endl;
        int numThreads=5;
        if (argc > 1){
            numThreads=atoi(argv[1]);
        }
        vector<MyRunnable*> runnables;
        for (int i=0;i< numThreads;i++){
            MyRunnable *runnable=new MyRunnable(i);
            Thread *t=new Thread(runnable);
            t->start();
            runnables.push_back(runnable);
        }
        //"misuse" of a condition for ms waits
        Condition waiter;
        //waiting for start
        bool allRunning=false;
        cout << "waiting for threads to start" << endl;
        while (! allRunning){
            allRunning=true;
            vector<MyRunnable*>::iterator it=runnables.begin();
            while (it != runnables.end()){
                if ((*it)->getState() != RUNNING) {
                    cout << "##state "<<((*it)->getId())<<" is "<<((*it)->getState())<<endl;
                    allRunning=false;
                }
                it++;
            }
            waiter.wait(20); //avoid busy loop
        }
        cout << numThreads << " are running" <<endl;
        waiter.wait(10000);
        cout << "stopping" << endl;
        vector<MyRunnable*>::iterator it=runnables.begin();
        while (it != runnables.end()){
            (*it)->setState(STOP);
            it++;
        }
        bool allStopped=false;
        int maxWait=50*10; //max 10s
        while (! allStopped && maxWait){
            allStopped=true;
            vector<MyRunnable*>::iterator it=runnables.begin();
            while (it != runnables.end()){
                if ((*it)->getState() != STOP) allStopped=false;
                it++;
            }
            maxWait--;
            waiter.wait(20); //avoid busy loop
        }
        if (allStopped) cout << "all threads stopped" << endl;
        else cout << "unable to stop all threads" << endl;
        //if we would like to wait here for all threads
        //we also would need to keep the thread objects...
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rbx
movl %edi, %r14d
movq 0x2cbe(%rip), %rdi      # 0x4fd8
leaq 0xce3(%rip), %rsi       # 0x3004
callq 0x2100
movq %rax, %rdi
callq 0x2080
cmpl $0x2, %r14d
jl 0x233f
movq 0x8(%rbx), %rdi
callq 0x2160
jmp 0x2342
pushq $0x5
popq %rax
xorps %xmm0, %xmm0
leaq -0x50(%rbp), %rcx
andq $0x0, 0x10(%rcx)
xorl %r14d, %r14d
testl %eax, %eax
movl $0x0, %r15d
movl %eax, -0x2c(%rbp)
cmovgl %eax, %r15d
movaps %xmm0, (%rcx)
pushq $0x10
popq %r12
leaq -0x90(%rbp), %r13
cmpl %r14d, %r15d
je 0x23b9
pushq $0x48
popq %rdi
callq 0x2110
movq %rax, %rbx
movq %rax, %rdi
movl %r14d, %esi
callq 0x25c6
movq %rbx, -0x90(%rbp)
movq %r12, %rdi
callq 0x2110
andq $0x0, 0x8(%rax)
movq %rbx, (%rax)
movq %rax, %rdi
callq 0x261e
leaq -0x50(%rbp), %rdi
movq %r13, %rsi
callq 0x266e
incl %r14d
jmp 0x236e
leaq -0x90(%rbp), %rdi
callq 0x2692
movq 0x2c0c(%rip), %rdi      # 0x4fd8
leaq 0xc3d(%rip), %rsi       # 0x3010
callq 0x2100
movq %rax, %rdi
callq 0x2080
movq 0x2bf1(%rip), %r12      # 0x4fd8
leaq 0xc3f(%rip), %r13       # 0x302d
leaq 0xc41(%rip), %rbx       # 0x3036
movb $0x1, %r14b
movq -0x50(%rbp), %r15
cmpq -0x48(%rbp), %r15
je 0x2459
movq (%r15), %rdi
callq 0x26e2
cmpl $0x1, %eax
je 0x2453
movq %r12, %rdi
movq %r13, %rsi
callq 0x2100
movq (%r15), %rcx
movl 0x40(%rcx), %esi
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2100
movq %rax, %r14
movq (%r15), %rdi
callq 0x26e2
movq %r14, %rdi
movl %eax, %esi
callq 0x2190
movq %rax, %rdi
callq 0x2080
xorl %r14d, %r14d
addq $0x8, %r15
jmp 0x23fc
leaq -0x90(%rbp), %rdi
pushq $0x14
popq %rsi
callq 0x2716
testb $0x1, %r14b
je 0x23f5
movq 0x2b63(%rip), %rdi      # 0x4fd8
movl -0x2c(%rbp), %esi
callq 0x2190
leaq 0xbb7(%rip), %rsi       # 0x303b
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
callq 0x2080
leaq -0x90(%rbp), %rdi
movl $0x2710, %esi           # imm = 0x2710
callq 0x2716
movq 0x2b2c(%rip), %rdi      # 0x4fd8
leaq 0xb95(%rip), %rsi       # 0x3048
callq 0x2100
movq %rax, %rdi
callq 0x2080
movq -0x50(%rbp), %r14
pushq $0x2
popq %rbx
cmpq -0x48(%rbp), %r14
je 0x24dd
movq (%r14), %rdi
movl %ebx, %esi
callq 0x2778
addq $0x8, %r14
jmp 0x24c7
movl $0x1f4, %r12d           # imm = 0x1F4
xorl %r15d, %r15d
leaq -0x90(%rbp), %rbx
pushq $0x14
popq %r14
subl $0x1, %r12d
setb %al
testb $0x1, %r15b
jne 0x2534
testb %al, %al
jne 0x2534
movb $0x1, %r15b
movq -0x50(%rbp), %r13
cmpq -0x48(%rbp), %r13
je 0x2527
movq (%r13), %rdi
callq 0x26e2
cmpl $0x2, %eax
sete %al
andb %al, %r15b
addq $0x8, %r13
jmp 0x2509
movq %rbx, %rdi
movl %r14d, %esi
callq 0x2716
jmp 0x24f1
leaq 0xb16(%rip), %rax       # 0x3051
leaq 0xb23(%rip), %rsi       # 0x3065
testb $0x1, %r15b
cmovneq %rax, %rsi
movq 0x2a87(%rip), %rdi      # 0x4fd8
callq 0x2100
movq %rax, %rdi
callq 0x2080
leaq -0x90(%rbp), %rdi
callq 0x27be
leaq -0x50(%rbp), %rdi
callq 0x2b48
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x259f
jmp 0x25a6
jmp 0x25a6
jmp 0x25a6
jmp 0x25a6
movq %rax, %r14
movq %rbx, %rdi
callq 0x20f0
jmp 0x25b5
jmp 0x259f
jmp 0x25a6
movq %rax, %r14
jmp 0x25b5
jmp 0x25a6
movq %rax, %r14
leaq -0x90(%rbp), %rdi
callq 0x27be
leaq -0x50(%rbp), %rdi
callq 0x2b48
movq %r14, %rdi
callq 0x21a0