Reputation: 33
I am facing one issue in which i have two tasks running, one is parent task and one is child task and child task is running one infinite loop and in which some condition is satisfied i want to terminate the child execution manually . How i can do in TBB
Upvotes: 0
Views: 894
Reputation: 970
Do you want to kill the task abruptly, or just make it leave the loop nicely, sop the task can end by itself?
For the first case you can probably use the destroy method
The second case is probably the better way to structure your code. Let's say your loop has a condition like
while(!finished) {
...
}
You can change this to use a lock on the finished
variable, so that you can also access it from outside the task, and change its value when you want the task to end. You just have to use the following functions to read and write the value instead of accessing the variable directly, both outside and inside the task.
tbb::mutex lock;
bool finished;
void finish() {
lock.lock();
finished = true;
lock.unlock();
}
bool is_finished() {
lock.lock();
bool ret = finished;
lock.unlock();
return ret;
}
So the loop condition would now be while(! is_finished())
And somewhere in the main thread you just call task.finish()
when you want it to end
Upvotes: 1