-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example.cpp
71 lines (61 loc) · 1.93 KB
/
Example.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <chrono>
#include "JobSystem.h"
using namespace std::chrono_literals;
int main()
{
job_system::JobSystem jobSystem;
jobSystem.AddJob([]() {
std::cout << "(1) Start working..." << std::endl;
std::this_thread::sleep_for(300ms);
std::cout << "(1) Work done" << std::endl;
}, []() {
std::cout << "(1) Callback" << std::endl;
});
jobSystem.AddJob<int>([]() {
std::cout << "(2) Start working..." << std::endl;
std::this_thread::sleep_for(1500ms);
std::cout << "(2) Work done" << std::endl;
return 4;
}, [](int result) {
std::cout << "(2) Callback, result: " << result << std::endl;
});
auto&& job3 = jobSystem.AddJob([]() {
std::cout << "(3) Start working..." << std::endl;
std::this_thread::sleep_for(500ms);
std::cout << "(3) Work done" << std::endl;
});
std::cout << "Waiting for job3 to finish" << std::endl;
job3.Wait();
auto&& job4 = jobSystem.AddJob<int>([]() {
std::cout << "(4) Start working..." << std::endl;
std::this_thread::sleep_for(100ms);
std::cout << "(4) Work done" << std::endl;
return 42;
});
std::cout << "Waiting for job4..." << std::endl;
job4.Wait();
std::cout << "Job4 finished, result: " << job4.GetResult() << std::endl;
jobSystem.AddJob([]() {
std::cout << "(5) Start working..." << std::endl;
std::this_thread::sleep_for(100ms);
std::cout << "(5) Work done" << std::endl;
}, [&]() {
std::cout << "(5) Callback - pre" << std::endl;
jobSystem.AddJob([]() {
std::cout << "(6) Start working..." << std::endl;
std::this_thread::sleep_for(125ms);
std::cout << "(6) Work done" << std::endl;
}, []() {
std::cout << "(6) Callback" << std::endl;
});
std::cout << "(5) Callback - post" << std::endl;
});
// This can be the game loop
while (jobSystem.HasJobs())
{
jobSystem.Tick();
}
std::cout << "\n=== Rest of the application here ===\n" << std::endl;
return 0;
}