// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %rsi, %rbx
movl %edi, %r14d
movq 0x2c8e(%rip), %r15      # 0x4fd8
leaq 0xcb3(%rip), %rsi       # 0x3004
movl $0xb, %edx
movq %r15, %rdi
callq 0x2130
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movl $0x5, %r13d
cmpl $0x2, %r14d
jl 0x239d
movq 0x8(%rbx), %rdi
callq 0x2180
movl %eax, %r13d
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
testl %r13d, %r13d
jle 0x2473
xorl %r14d, %r14d
movl $0x48, %edi
callq 0x2120
movq %rax, %r15
leaq 0x297c(%rip), %rax      # 0x4d48
movq %rax, (%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r15)
movups %xmm0, 0x18(%r15)
xorl %eax, %eax
movq %rax, 0x28(%r15)
movq %rax, 0x38(%r15)
movl %r14d, 0x40(%r15)
movl $0x40, %edi
callq 0x2120
movq %rax, %r12
movq %r15, %rbx
addq $0x8, %rbx
movq %rax, %rdi
addq $0x8, %rdi
callq 0x2150
movb $0x0, 0x38(%r12)
movq %rbx, (%r12)
movq %r12, 0x30(%r15)
movq %r15, -0xa0(%rbp)
movl $0x10, %edi
callq 0x2120
movq $0x0, 0x8(%rax)
movq %r15, (%rax)
movq %rax, %rdi
callq 0x27f8
movq -0x48(%rbp), %rsi
cmpq -0x40(%rbp), %rsi
je 0x2457
movq -0xa0(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, -0x48(%rbp)
jmp 0x2467
leaq -0x50(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x2d00
incl %r14d
cmpl %r14d, %r13d
jne 0x23b8
leaq -0xa0(%rbp), %rdi
callq 0x284a
movl %r13d, -0x2c(%rbp)
movq 0x2b4e(%rip), %rbx      # 0x4fd8
leaq 0xb7f(%rip), %rsi       # 0x3010
movl $0x1c, %edx
movq %rbx, %rdi
callq 0x2130
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq 0x2b1c(%rip), %rdi      # 0x4fd8
callq 0x2030
movq %rax, %rdi
callq 0x20b0
xorl %r14d, %r14d
leaq -0x60(%rbp), %r15
movq 0x2b01(%rip), %r12      # 0x4fd8
testb $0x1, %r14b
jne 0x25e4
movq -0x50(%rbp), %rbx
cmpq -0x48(%rbp), %rbx
je 0x25cb
movb $0x1, %r14b
movq (%rbx), %r13
leaq 0x8(%r13), %rax
movq %rax, -0x60(%rbp)
movb $0x0, -0x58(%rbp)
movq %r15, %rdi
callq 0x2b58
movb $0x1, -0x58(%rbp)
movl 0x3c(%r13), %r13d
movq %r15, %rdi
callq 0x2b3e
cmpl $0x1, %r13d
je 0x25bb
movl $0x8, %edx
movq %r12, %rdi
leaq 0xafb(%rip), %rsi       # 0x302d
callq 0x2130
movq (%rbx), %rax
movl 0x40(%rax), %esi
movq %r12, %rdi
callq 0x21b0
movq %rax, %r14
movl $0x4, %edx
movq %rax, %rdi
leaq 0xadf(%rip), %rsi       # 0x3036
callq 0x2130
movq (%rbx), %r13
leaq 0x8(%r13), %rax
movq %rax, -0x60(%rbp)
movb $0x0, -0x58(%rbp)
movq %r15, %rdi
callq 0x2b58
movb $0x1, -0x58(%rbp)
movl 0x3c(%r13), %r13d
movq %r15, %rdi
callq 0x2b3e
movq %r14, %rdi
movl %r13d, %esi
callq 0x21b0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
xorl %r14d, %r14d
addq $0x8, %rbx
cmpq -0x48(%rbp), %rbx
jne 0x24f2
jmp 0x25ce
movb $0x1, %r14b
leaq -0xa0(%rbp), %rdi
movl $0x14, %esi
callq 0x28a0
jmp 0x24d7
movq 0x29ed(%rip), %rdi      # 0x4fd8
movl -0x2c(%rbp), %esi
callq 0x21b0
movq %rax, %rbx
leaq 0xa3e(%rip), %rsi       # 0x303b
movl $0xc, %edx
movq %rax, %rdi
callq 0x2130
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
leaq -0xa0(%rbp), %rdi
movl $0x2710, %esi           # imm = 0x2710
callq 0x28a0
movq 0x298f(%rip), %rbx      # 0x4fd8
leaq 0x9f8(%rip), %rsi       # 0x3048
movl $0x8, %edx
movq %rbx, %rdi
callq 0x2130
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq 0x295d(%rip), %rdi      # 0x4fd8
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq -0x50(%rbp), %rbx
cmpq -0x48(%rbp), %rbx
je 0x26a5
movq (%rbx), %rdi
movl $0x2, %esi
callq 0x291e
addq $0x8, %rbx
jmp 0x268c
movl $0x1f4, %r15d           # imm = 0x1F4
leaq -0x60(%rbp), %r14
xorl %r12d, %r12d
movq -0x50(%rbp), %rbx
cmpq -0x48(%rbp), %rbx
je 0x2706
movl %r15d, -0x2c(%rbp)
movb $0x1, %r13b
movq (%rbx), %r15
leaq 0x8(%r15), %rax
movq %rax, -0x60(%rbp)
movb $0x0, -0x58(%rbp)
movq %r14, %rdi
callq 0x2b58
movb $0x1, -0x58(%rbp)
movl 0x3c(%r15), %r15d
movq %r14, %rdi
callq 0x2b3e
cmpl $0x2, %r15d
movzbl %r13b, %r13d
cmovnel %r12d, %r13d
addq $0x8, %rbx
cmpq -0x48(%rbp), %rbx
jne 0x26c3
movl -0x2c(%rbp), %r15d
jmp 0x2709
movb $0x1, %r13b
leaq -0xa0(%rbp), %rdi
movl $0x14, %esi
callq 0x28a0
decl %r15d
sete %al
orb %r13b, %al
testb $0x1, %al
je 0x26b2
leaq 0x923(%rip), %rax       # 0x3051
leaq 0x930(%rip), %rsi       # 0x3065
testb $0x1, %r13b
cmovneq %rax, %rsi
movl $0x13, %eax
movl $0x1a, %edx
cmovneq %rax, %rdx
movq 0x2886(%rip), %rbx      # 0x4fd8
movq %rbx, %rdi
callq 0x2130
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2110
movsbl %al, %esi
movq 0x2860(%rip), %rdi      # 0x4fd8
callq 0x2030
movq %rax, %rdi
callq 0x20b0
leaq -0xa0(%rbp), %rdi
callq 0x2972
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x279f
callq 0x2100
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x27cb
jmp 0x27d2
jmp 0x27d2
jmp 0x27d2
jmp 0x27cb
movq %rax, %rbx
movq %r15, %rdi
callq 0x2100
jmp 0x27e1
jmp 0x27d2
jmp 0x27d2
movq %rax, %rbx
jmp 0x27e1
jmp 0x27d2
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x2972
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x27ef
callq 0x2100
movq %rbx, %rdi
callq 0x21c0
nop