-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench1mtfg.cpp
More file actions
51 lines (48 loc) · 1 KB
/
Copy pathbench1mtfg.cpp
File metadata and controls
51 lines (48 loc) · 1 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
44
45
46
47
48
49
50
51
#include <unordered_map>
#include <iostream>
#include <future>
#include <thread>
#include <mutex>
const int SIZE = 1000000;
struct data_t
{
std::unordered_map<int,int> map;
std::mutex mutex;
char pad[64];
data_t()
{
map.reserve(2*SIZE/100);
}
};
data_t& data(unsigned n)
{
static data_t datas[100];
return datas[n % 100];
}
int main(int argc, char *argv[])
{
const int thcount = (argc > 1 && atoi(argv[1]) > 0) ? atoi(argv[1]) : 1;
//
std::future<long long> fu[thcount];
for (int k = 0; k < thcount; k++)
{
fu[k] = std::async(std::launch::async, [&](int kk)-> long long
{
long long c = 0;
for (int i = kk; i < SIZE; i += thcount)
{
data_t &d = data(i*i);
std::lock_guard<std::mutex> l(d.mutex);
c += d.map[i*i] += i;
}
return c;
}, k);
}
//
long long c = 0;
for (int k = 0; k < thcount; k++)
c += fu[k].get();
//
std::cout << c << std::endl;
return 0;
}