-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmultithread_futures.cpp
46 lines (39 loc) · 1.02 KB
/
multithread_futures.cpp
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
44
45
46
// Observable Library
// Copyright (c) 2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "obs/signal.h"
#include "test.h"
#include <algorithm>
#include <ctime>
#include <future>
#include <thread>
#include <vector>
int main() {
obs::signal<void()> sig;
std::time_t t = std::time(nullptr);
auto func =
[&sig, t](){
int count = 0;
while ((std::time(nullptr) - t) < 5) {
std::vector<obs::scoped_connection> conns(32);
for (auto& conn : conns) {
conn = sig.connect([](){ });
++count;
if (std::time(nullptr) - t >= 5)
break;
}
}
return count;
};
std::vector<std::future<int>> futures;
int n = std::max<int>(1, std::thread::hardware_concurrency()/2);
while (n--)
futures.emplace_back(std::async(std::launch::async, func));
int count = 0;
for (auto& f : futures)
count += f.get();
std::cout << "Count = " << count << "\n";
return 0;
}