File: mutex.cpp

package info (click to toggle)
yrmcds 1.1.9-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 940 kB
  • sloc: cpp: 11,149; sh: 148; makefile: 117
file content (28 lines) | stat: -rw-r--r-- 452 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cybozu/test.hpp>

#include <mutex>
#include <thread>
#include <iostream>

std::mutex lock;

int g_counter = 0;

void foo() {
    for (int i = 0; i < 10000; ++i) {
        std::lock_guard<std::mutex> g(lock);
        ++g_counter;
    }
    return;
}

AUTOTEST(mutex) {
    std::thread t1(foo);
    std::thread t2(foo);
    std::thread t3(foo);
    foo();
    t1.join();
    t2.join();
    t3.join();
    cybozu_assert( g_counter == 40000 );
}