-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
62 lines (56 loc) · 1.75 KB
/
test.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
#include "tq.h"
#include <iostream>
/**
* Test implementation of a task
* The processing function sleeps for a given timeout in order to simulate long processing time
* Not that the queue accepts any task built on top of tq::task
*/
class MyTask : public maxy::tq::task
{
int x;
int timeout;
public:
void operator() () noexcept
{
if (timeout <= 0)
{
std::cout << "TASK (" << x << ") ERROR!\n";
return;
}
std::cout << "TASK (" << x << ") processing start\n";
std::this_thread::sleep_for (std::chrono::milliseconds (timeout));
std::cout << "TASK (" << x << ") processing end\n";
};
MyTask (int xx, int tt) : x {xx}, timeout {tt} { std::cout << "TASK (" << x << ") construction\n"; };
~MyTask () { std::cout << "TASK (" << x << ") destruction\n"; };
};
/**
* Helper function that just waits for 100 ms
*/
void tick ()
{
std::this_thread::sleep_for (std::chrono::milliseconds (100));
std::cout << "--- Main thread tick\n";
}
/**
* Main function simulates the operation of the queue.
* It works for a number of 'time frames' and during each frame it posts some tasks to the queue
* Note the processing of tasks takes longer than the execution time of the whole main() function
*/
int main()
{
maxy::tq q {std::chrono::milliseconds (50)};
q.post (new MyTask {1, 100});
tick ();
q.post (new MyTask {2, 200});
tick ();
tick ();
q.post (new MyTask {3, 100});
q.post (nullptr);
q.post (new MyTask {4, 200});
q.post (new MyTask {5, 0}); // this will generate an error
tick ();
q.post (new MyTask {6, 100});
tick ();
std::cout << "--- Main thread termination\n";
}