I ran your pool:pool_of_n_t(2) test on Linux and saw a problem in
this_thread::yield();
See the results on my comp for the test pool: pool_of_n_t (2):
1) this_thread :: yield ():
$./a.out pool:pool_of_n_t(2) thread 0, run for:2053 ms thread 9, run for:3721 ms thread 5, run for:4830 ms thread 6, run for:6854 ms thread 3, run for:8229 ms thread 4, run for:8353 ms thread 7, run for:9441 ms thread 2, run for:9482 ms thread 1, run for:10127 ms thread 8, run for:10426 ms
They are like yours.
2) And the same test when I replace this_thread::yield() with pthread_yield() :
$ ./a.out pool:pool_of_n_t(2) thread 0, run for:7922 ms thread 3, run for:8853 ms thread 4, run for:8854 ms thread 1, run for:9077 ms thread 5, run for:9364 ms thread 9, run for:9446 ms thread 7, run for:9594 ms thread 2, run for:9615 ms thread 8, run for:10170 ms thread 6, run for:10416 ms
This is much more true. You assume that this_thread :: yield () does give the CPU to another thread, but it does not.
This is the disas for this_thread :: yield for gcc 4.8:
(gdb) disassembl this_thread::yield Dump of assembler code for function std::this_thread::yield(): 0x0000000000401fb2 <+0>: push %rbp 0x0000000000401fb3 <+1>: mov %rsp,%rbp 0x0000000000401fb6 <+4>: pop %rbp 0x0000000000401fb7 <+5>: retq End of assembler dump.
I do not see a rescheduling
And these are the disas for pthread_yield:
(gdb) disassemble pthread_yield Dump of assembler code for function pthread_yield: 0x0000003149c084c0 <+0>: jmpq 0x3149c05448 < sched_yield@plt > End of assembler dump. (gdb) disassemble sched_yield Dump of assembler code for function sched_yield: 0x00000031498cf520 <+0>: mov $0x18,%eax 0x00000031498cf525 <+5>: syscall 0x00000031498cf527 <+7>: cmp $0xfffffffffffff001,%rax 0x00000031498cf52d <+13>: jae 0x31498cf530 <sched_yield+16> 0x00000031498cf52f <+15>: retq 0x00000031498cf530 <+16>: mov 0x2bea71(%rip),%rcx # 0x3149b8dfa8 0x00000031498cf537 <+23>: xor %edx,%edx 0x00000031498cf539 <+25>: sub %rax,%rdx 0x00000031498cf53c <+28>: mov %edx,%fs:(%rcx) 0x00000031498cf53f <+31>: or $0xffffffffffffffff,%rax 0x00000031498cf543 <+35>: jmp 0x31498cf52f <sched_yield+15> End of assembler dump.