// File: guoxuanhan[P]NetServer/threadpool.cpp
// Function: ThreadPool::ThreadFunc()
void ThreadPool::ThreadFunc()
{
    std::thread::id tid = std::this_thread::get_id();
    std::stringstream sin;
    sin << tid;
    std::cout<< "Worker thread is running! tid: " << tid << std::endl;
    Task task;
    while(started_)
    {
        task = NULL;
        {
            std::unique_lock<std::mutex> lock(mutex_);//unique_lock支持解锁又上锁的情况
            while(taskqueue_.empty() && started_)
            {
                condition_.wait(lock);
            }
            if(!started_)
            {
                break;
            }
            std::cout<< "Thread wake up! tid: " << tid << std::endl;
            std::cout<< "Taskqueue size: " << taskqueue_.size() << std::endl;
            task = taskqueue_.front();
            taskqueue_.pop();
        }
        if(task)
        {
            try
            {
                task();//task()中IO过程可以使用协程优化，让出CPU资源
            } catch(std::bad_alloc& bad)
            {
                std::cerr << "Bad_alloc caught in ThreadPool::ThreadFunc task: " << bad.what() << std::endl;
                while(true);
            }
        }
    }
}

// Assembly:
subq $0x268, %rsp            # imm = 0x268
movq %rdi, 0x260(%rsp)
movq 0x260(%rsp), %rax
movq %rax, 0x60(%rsp)
callq 0x8940
movq %rax, 0x258(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x52c0
leaq 0xe0(%rsp), %rdi
movq 0x258(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0xc8(%rsp), %rsi
callq 0xa970
jmp 0x2336d
movq 0x17c3c(%rip), %rdi     # 0x3afb0
leaq 0x627c(%rip), %rsi      # 0x295f7
callq 0x53a0
movq %rax, 0x58(%rsp)
jmp 0x23387
movq 0x58(%rsp), %rdi
movq 0x258(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rsi
callq 0xa970
movq %rax, 0x50(%rsp)
jmp 0x233b0
movq 0x50(%rsp), %rdi
movq 0x17bc4(%rip), %rsi     # 0x3af80
callq 0x53f0
jmp 0x233c3
leaq 0x90(%rsp), %rdi
callq 0x5eb0
movq 0x60(%rsp), %rax
testb $0x1, (%rax)
je 0x236af
xorl %eax, %eax
movl %eax, %esi
leaq 0x90(%rsp), %rdi
callq 0x23a80
movq 0x60(%rsp), %rsi
addq $0x70, %rsi
leaq 0x80(%rsp), %rdi
callq 0x23ae0
jmp 0x23407
jmp 0x23409
movq 0x60(%rsp), %rdi
addq $0x20, %rdi
callq 0x23b20
movb %al, 0x4f(%rsp)
jmp 0x2341d
movb 0x4f(%rsp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x4e(%rsp)
jne 0x2342e
jmp 0x23439
movq 0x60(%rsp), %rax
movb (%rax), %al
movb %al, 0x4e(%rsp)
movb 0x4e(%rsp), %al
testb $0x1, %al
jne 0x23443
jmp 0x234b6
movq 0x60(%rsp), %rdi
addq $0x98, %rdi
leaq 0x80(%rsp), %rsi
callq 0x5110
jmp 0x23409
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
jmp 0x236de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
jmp 0x236d1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x23b70
jmp 0x236d1
movq 0x60(%rsp), %rax
testb $0x1, (%rax)
jne 0x234cd
movl $0x3, 0x7c(%rsp)
jmp 0x235b6
movq 0x17adc(%rip), %rdi     # 0x3afb0
leaq 0x613c(%rip), %rsi      # 0x29617
callq 0x53a0
movq %rax, 0x40(%rsp)
jmp 0x234e7
movq 0x40(%rsp), %rdi
movq 0x258(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rsi
callq 0xa970
movq %rax, 0x38(%rsp)
jmp 0x2350a
movq 0x38(%rsp), %rdi
movq 0x17a6a(%rip), %rsi     # 0x3af80
callq 0x53f0
jmp 0x2351d
movq 0x17a8c(%rip), %rdi     # 0x3afb0
leaq 0x6102(%rip), %rsi      # 0x2962d
callq 0x53a0
movq %rax, 0x30(%rsp)
jmp 0x23537
movq 0x60(%rsp), %rdi
addq $0x20, %rdi
callq 0x23b40
movq %rax, 0x28(%rsp)
jmp 0x2354c
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdi
callq 0x50c0
movq %rax, 0x20(%rsp)
jmp 0x23562
movq 0x20(%rsp), %rdi
movq 0x17a12(%rip), %rsi     # 0x3af80
callq 0x53f0
jmp 0x23575
movq 0x60(%rsp), %rdi
addq $0x20, %rdi
callq 0x23b50
movq %rax, 0x18(%rsp)
jmp 0x2358a
movq 0x18(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x5f40
jmp 0x2359e
movq 0x60(%rsp), %rdi
addq $0x20, %rdi
callq 0x23b60
jmp 0x235ae
movl $0x0, 0x7c(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x23b70
movl 0x7c(%rsp), %eax
testl %eax, %eax
je 0x235d2
jmp 0x235cd
jmp 0x236af
leaq 0x90(%rsp), %rdi
callq 0x6130
testb $0x1, %al
jne 0x235e8
jmp 0x236aa
leaq 0x90(%rsp), %rdi
callq 0x5f00
jmp 0x235f7
jmp 0x236a8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x236d1
movq 0xc0(%rsp), %rdi
callq 0x5180
movq %rax, 0x68(%rsp)
movq 0x179a3(%rip), %rdi     # 0x3afe0
leaq 0x5ffa(%rip), %rsi      # 0x2963e
callq 0x53a0
movq %rax, 0x10(%rsp)
jmp 0x23650
movq 0x68(%rsp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x53a0
movq %rax, 0x8(%rsp)
jmp 0x23672
movq 0x8(%rsp), %rdi
movq 0x17902(%rip), %rsi     # 0x3af80
callq 0x53f0
jmp 0x23685
jmp 0x23687
jmp 0x23689
jmp 0x23687
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
callq 0x55e0
jmp 0x236a6
jmp 0x236d1
jmp 0x236aa
jmp 0x233d0
leaq 0x90(%rsp), %rdi
callq 0x5ef0
leaq 0xd0(%rsp), %rdi
callq 0x52e0
addq $0x268, %rsp            # imm = 0x268
retq
leaq 0x90(%rsp), %rdi
callq 0x5ef0
leaq 0xd0(%rsp), %rdi
callq 0x52e0
movq 0xc0(%rsp), %rdi
callq 0x5660
movq %rax, %rdi
callq 0x5fd0