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

    for(int i = 0; i < REPEAT; ++i){
        std::atomic<int> counter;
        counter.store(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){
                    ++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 << "atomic with " << Threads << " threads throughput = " << (throughput / REPEAT) << std::endl;
}

// Assembly:
subq $0xf8, %rsp
leaq 0xb8(%rsp), %rdi
callq 0x6420
movq $0x0, 0xb0(%rsp)
movl $0x0, 0xac(%rsp)
cmpl $0x5, 0xac(%rsp)
jge 0x4dc7
leaq 0xa8(%rsp), %rax
movq %rax, 0xf0(%rsp)
movl $0x0, 0xec(%rsp)
movl $0x5, 0xe8(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x18(%rsp)
movl 0xe8(%rsp), %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0xade0
movl %eax, 0x24(%rsp)
jmp 0x4ba3
movl 0x24(%rsp), %eax
movl %eax, 0xe4(%rsp)
movl 0xe8(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0xec(%rsp), %ecx
movl %ecx, 0xe0(%rsp)
subl $0x3, %eax
je 0x4be9
jmp 0x4bce
movl 0x14(%rsp), %eax
subl $0x5, %eax
je 0x4bf9
jmp 0x4bd9
movq 0x18(%rsp), %rax
movl 0xe0(%rsp), %ecx
movl %ecx, (%rax)
jmp 0x4c11
movq 0x18(%rsp), %rax
movl 0xe0(%rsp), %ecx
movl %ecx, (%rax)
jmp 0x4c11
movq 0x18(%rsp), %rcx
movl 0xe0(%rsp), %eax
xchgl %eax, (%rcx)
jmp 0x4c11
movq %rax, %rdi
callq 0x6e80
leaq 0x90(%rsp), %rdi
callq 0x6430
callq 0x2030
movq %rax, 0x88(%rsp)
movl $0x0, 0x84(%rsp)
cmpl $0x1, 0x84(%rsp)
jge 0x4cbf
leaq 0xa8(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq 0x78(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0xad20
jmp 0x4c5e
leaq 0x90(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x6440
jmp 0x4c72
leaq 0x78(%rsp), %rdi
callq 0x6530
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x4c36
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
jmp 0x4db8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x6530
jmp 0x4db8
leaq 0x90(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rdi
callq 0x6550
movq %rax, 0x50(%rsp)
movq 0x58(%rsp), %rdi
callq 0x6580
movq %rax, 0x48(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x65b0
testb $0x1, %al
jne 0x4cff
jmp 0x4d28
leaq 0x50(%rsp), %rdi
callq 0x65f0
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rdi
callq 0x2160
jmp 0x4d1a
jmp 0x4d1c
leaq 0x50(%rsp), %rdi
callq 0x6600
jmp 0x4cea
callq 0x2030
movq %rax, 0x38(%rsp)
leaq 0x38(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x6650
movq %rax, 0x8(%rsp)
jmp 0x4d4b
movq 0x8(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x6620
movq %rax, (%rsp)
jmp 0x4d65
movq (%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x66a0
movq %rax, %rcx
movl $0x3d090, %eax          # imm = 0x3D090
cqto
idivq %rcx
addq 0xb0(%rsp), %rax
movq %rax, 0xb0(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x66b0
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x4b4b
leaq 0x90(%rsp), %rdi
callq 0x66b0
jmp 0x4e2a
movq 0x1020a(%rip), %rdi     # 0x14fd8
leaq 0x827c(%rip), %rsi      # 0xd051
callq 0x2110
movq %rax, %rdi
movl $0x1, %esi
callq 0x2170
movq %rax, %rdi
leaq 0x821e(%rip), %rsi      # 0xd00f
callq 0x2110
movq %rax, %rdi
movq 0xb0(%rsp), %rax
movl $0x5, %ecx
xorl %edx, %edx
divq %rcx
movq %rax, %rsi
callq 0x2040
movq %rax, %rdi
movq 0x101a3(%rip), %rsi     # 0x14fc0
callq 0x2130
addq $0xf8, %rsp
retq
movq 0x68(%rsp), %rdi
callq 0x2180
nopw %cs:(%rax,%rax)
nop