-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpass_shared_syncronised_string.cpp
More file actions
43 lines (35 loc) · 1.17 KB
/
pass_shared_syncronised_string.cpp
File metadata and controls
43 lines (35 loc) · 1.17 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <functional>
#include <memory>
#include <print>
#include <ranges>
#include <scl/safe_thread.h>
#include <scl/synchronized_value.h>
using namespace std::literals;
void entry_point (std::shared_ptr<scl::synchronized_value<std::string>> sync_s, int tid)
{
apply ( [tid] (auto& s) {
s.append ("🔥");
std::println ("{} {}", s, tid);
return s;
},
*sync_s);
}
static_assert(scl::is_function_pointer_v<std::decay_t<decltype(entry_point)>>);
static_assert(! std::is_member_function_pointer_v<std::decay_t<decltype(entry_point)>>);
static_assert(! std::is_member_function_pointer_v<decltype(entry_point)>);
static_assert(scl::is_send_v<decltype(entry_point)>);
int main()
{
std::vector<scl::thread> threads { };
{
//s dies before the threads join, so possible
auto s = std::make_shared<scl::synchronized_value<std::string>> ("Hello threads");
// Launch all threads.
const int num_threads = 15;
for (int i : std::views::iota (0, num_threads))
threads.push_back (scl::thread (entry_point, auto (s), auto (i)));
}
// Join all threads.
for (scl::thread& t : threads)
t.join();
}