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

    for(int i = 0; i < REPEAT; ++i){
        std::vector<std::thread> threads;
        int counter = 0;

        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){
                    mutex.lock();
                    ++counter;
                    mutex.unlock();
                }
            }));
        }

        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 with " << Threads << " threads throughput = " << (throughput / REPEAT) << std::endl;
}

// Assembly:
subq $0xe8, %rsp
leaq 0xc0(%rsp), %rdi
callq 0x6420
movq $0x0, 0xb8(%rsp)
movl $0x0, 0xb4(%rsp)
cmpl $0x5, 0xb4(%rsp)
jge 0x2606
leaq 0x98(%rsp), %rdi
callq 0x6430
movl $0x0, 0x94(%rsp)
callq 0x2030
movq %rax, 0x88(%rsp)
movl $0x0, 0x84(%rsp)
cmpl $0x1, 0x84(%rsp)
jge 0x24f3
leaq 0xc0(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x94(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq 0x78(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x6470
jmp 0x2492
leaq 0x98(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x6440
jmp 0x24a6
leaq 0x78(%rsp), %rdi
callq 0x6530
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x2459
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
jmp 0x25f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x6530
jmp 0x25f7
leaq 0x98(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rdi
callq 0x6550
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rdi
callq 0x6580
movq %rax, 0x40(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x65b0
testb $0x1, %al
jne 0x2533
jmp 0x255c
leaq 0x48(%rsp), %rdi
callq 0x65f0
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
callq 0x2160
jmp 0x254e
jmp 0x2550
leaq 0x48(%rsp), %rdi
callq 0x6600
jmp 0x251e
callq 0x2030
movq %rax, 0x30(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x6650
movq %rax, 0x18(%rsp)
jmp 0x257f
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x6620
movq %rax, 0x10(%rsp)
jmp 0x259a
movq 0x10(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x66a0
movq %rax, 0x8(%rsp)
jmp 0x25b5
movq 0x8(%rsp), %rcx
movl $0x3d090, %eax          # imm = 0x3D090
cqto
idivq %rcx
addq 0xb8(%rsp), %rax
movq %rax, 0xb8(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x66b0
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x241b
leaq 0x98(%rsp), %rdi
callq 0x66b0
jmp 0x2669
movq 0x129cb(%rip), %rdi     # 0x14fd8
leaq 0xa9f0(%rip), %rsi      # 0xd004
callq 0x2110
movq %rax, %rdi
movl $0x1, %esi
callq 0x2170
movq %rax, %rdi
leaq 0xa9df(%rip), %rsi      # 0xd00f
callq 0x2110
movq %rax, %rdi
movq 0xb8(%rsp), %rax
movl $0x5, %ecx
xorl %edx, %edx
divq %rcx
movq %rax, %rsi
callq 0x2040
movq %rax, %rdi
movq 0x12964(%rip), %rsi     # 0x14fc0
callq 0x2130
addq $0xe8, %rsp
retq
movq 0x60(%rsp), %rdi
callq 0x2180
nopw %cs:(%rax,%rax)
nopl (%rax)