// File: wichtounet[P]articles/src/threads/benchmark/bench.cpp
// Function: void bench_lock_guard<1>()
void bench_lock_guard(){
    std::mutex mutex;
    unsigned long throughput = 0;

    for(int i = 0; i < REPEAT; ++i){
        int counter = 0;

        std::vector<std::thread> threads;

        Clock::time_point t0 = Clock::now();

        for(int i = 0; i < Threads; ++i){
            threads.push_back(std::thread([&](){
                for(int i = 0; i < OPERATIONS; ++i){
                    std::lock_guard<std::mutex> guard(mutex);
                    ++counter;
                }
            }));
        }

        for(auto& thread : threads){
            thread.join();
        }

        Clock::time_point t1 = Clock::now();

        milliseconds ms = std::chrono::duration_cast<milliseconds>(t1 - t0);
        throughput += (Threads * OPERATIONS) / ms.count();
    }

    std::cout << "lock_guard with " << Threads << " threads throughput = " << (throughput / REPEAT) << std::endl;
}

// Assembly:
subq $0xd8, %rsp
leaq 0xb0(%rsp), %rdi
callq 0x6420
movq $0x0, 0xa8(%rsp)
movl $0x0, 0xa4(%rsp)
cmpl $0x5, 0xa4(%rsp)
jge 0x3991
movl $0x0, 0xa0(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x6430
callq 0x2030
movq %rax, 0x80(%rsp)
movl $0x0, 0x7c(%rsp)
cmpl $0x1, 0x7c(%rsp)
jge 0x3887
leaq 0xb0(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0xa0(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x9150
jmp 0x382c
leaq 0x88(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x6440
jmp 0x3840
leaq 0x70(%rsp), %rdi
callq 0x6530
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x37f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
jmp 0x3982
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x6530
jmp 0x3982
leaq 0x88(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
callq 0x6550
movq %rax, 0x40(%rsp)
movq 0x48(%rsp), %rdi
callq 0x6580
movq %rax, 0x38(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x65b0
testb $0x1, %al
jne 0x38c7
jmp 0x38f0
leaq 0x40(%rsp), %rdi
callq 0x65f0
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rdi
callq 0x2160
jmp 0x38e2
jmp 0x38e4
leaq 0x40(%rsp), %rdi
callq 0x6600
jmp 0x38b2
callq 0x2030
movq %rax, 0x28(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x6650
movq %rax, 0x10(%rsp)
jmp 0x3913
movq 0x10(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x6620
movq %rax, 0x8(%rsp)
jmp 0x392e
movq 0x8(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x66a0
movq %rax, %rcx
movl $0x3d090, %eax          # imm = 0x3D090
cqto
idivq %rcx
addq 0xa8(%rsp), %rax
movq %rax, 0xa8(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x66b0
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x37bb
leaq 0x88(%rsp), %rdi
callq 0x66b0
jmp 0x39f4
movq 0x11640(%rip), %rdi     # 0x14fd8
leaq 0x96a1(%rip), %rsi      # 0xd040
callq 0x2110
movq %rax, %rdi
movl $0x1, %esi
callq 0x2170
movq %rax, %rdi
leaq 0x9654(%rip), %rsi      # 0xd00f
callq 0x2110
movq %rax, %rdi
movq 0xa8(%rsp), %rax
movl $0x5, %ecx
xorl %edx, %edx
divq %rcx
movq %rax, %rsi
callq 0x2040
movq %rax, %rdi
movq 0x115d9(%rip), %rsi     # 0x14fc0
callq 0x2130
addq $0xd8, %rsp
retq
movq 0x58(%rsp), %rdi
callq 0x2180
nop