MCPcopy Create free account
hub / github.com/davisking/dlib / mytask

Method mytask

examples/thread_pool_ex.cpp:46–80  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

44public:
45
46 void mytask()
47 {
48 dlog << LINFO << "mytask start";
49
50 dlib::future<int> var;
51
52 var = 1;
53
54 // Here we ask the thread pool to call this->subtask() and this->subtask2().
55 // Note that calls to add_task() will return immediately if there is an
56 // available thread. However, if there isn't a thread ready then
57 // add_task() blocks until there is such a thread. Also, note that if
58 // mytask() is executed within the thread pool then calls to add_task()
59 // will execute the requested task within the calling thread in cases
60 // where the thread pool is full. This means it is always safe to spawn
61 // subtasks from within another task, which is what we are doing here.
62 tp.add_task(*this,&test::subtask,var); // schedule call to this->subtask(var)
63 tp.add_task(*this,&test::subtask2); // schedule call to this->subtask2()
64
65 // Since var is a future, this line will wait for the test::subtask task to
66 // finish before allowing us to access the contents of var. Then var will
67 // return the integer it contains. In this case result will be assigned
68 // the value 2 since var was incremented by subtask().
69 int result = var;
70 dlog << LINFO << "var = " << result;
71
72 // Wait for all the tasks we have started to finish. Note that
73 // wait_for_all_tasks() only waits for tasks which were started by the
74 // calling thread. So you don't have to worry about other unrelated
75 // parts of your application interfering. In this case it just waits
76 // for subtask2() to finish.
77 tp.wait_for_all_tasks();
78
79 dlog << LINFO << "mytask end" ;
80 }
81
82 void subtask(int& a)
83 {

Callers

nothing calls this directly

Calls 2

wait_for_all_tasksMethod · 0.80
add_taskMethod · 0.45

Tested by

no test coverage detected