-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathexample.cpp
More file actions
47 lines (38 loc) · 1.17 KB
/
example.cpp
File metadata and controls
47 lines (38 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
44
45
46
47
#include <iostream>
#include "task_queue.h"
#include <memory>
#include <thread>
#include "event.h"
#include "task_queue_manager.h"
using namespace std;
int main()
{
cout << "Hello World!" << endl;
TQMgr->create({"worker1", "worker2", "worker3"});
vi::Event ev;
const int N = 12;
std::thread threads[N];
for (int n = 0; n < N; ++n) {
threads[n] = std::thread([&ev]{
for (int i = 0; i < 10000; ++i) {
TQ("worker1")->postTask([&ev, i](){
cout << "exec task in 'worker1' queue: " << ", i = " << i << endl;
});
TQ("worker1")->postDelayedTask([&ev, i](){
cout << "exec delayed task in 'worker1' queue: " << ", i = " << i << endl;
}, 1000);
TQ("worker2")->postDelayedTask([&ev, i](){
cout << "exec delayed task in 'worker2' queue: " << ", i = " << i << endl;
if (i == 9999) {
ev.set();
}
}, 1000);
}
});
}
ev.wait(-1);
for (int n = 0; n < N; ++n) {
threads[n].join();
}
return 0;
}