Skip to content

Commit a5004f2

Browse files
authored
Update 003_std_atomic_long_long.cpp
1 parent 2ca7395 commit a5004f2

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

cpp_11/003_std_atomic_long_long.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1-
1
1+
#include <iostream>
2+
#include <thread>
3+
#include <atomic>
4+
#include <array>
5+
6+
std::atomic<long long> data{10};
7+
std::array<long long, 5> return_values{};
8+
9+
void do_work(int thread_num)
10+
{
11+
long long val = data.fetch_add(1, std::memory_order_relaxed);
12+
return_values[thread_num] = val;
13+
}
14+
15+
int main()
16+
{
17+
{
18+
std::jthread th0{do_work, 0};
19+
std::jthread th1{do_work, 1};
20+
std::jthread th2{do_work, 2};
21+
std::jthread th3{do_work, 3};
22+
std::jthread th4{do_work, 4};
23+
}
24+
25+
std::cout << "Result : " << data << '\n';
26+
27+
for (long long val : return_values) {
28+
std::cout << "Seen return value : " << val << std::endl;
29+
}
30+
}

0 commit comments

Comments
 (0)