External conditions: is my code safe?
mzient opened this issue · comments
Michał Zientkiewicz commented
Hello,
I'm about to start using taskflow in a large project and my workflow would be greatly simplified if there was a way for a task to wait for some external condition.
I wrote the following piece of code - it seems to work in a toy example, but is it safe?
tf::Executor exec;
tf::Semaphore sem{1};
tf::Taskflow f;
auto a = f.emplace([](){
sleep_s(0.5); printf("Tick\n");
}).acquire(sem);
// this task will have to wait, obviously
f.emplace([](){
printf("Tock\n");
}).acquire(sem).release(sem).succeed(a);
exec.run(f);
sleep_s(1.0);
std::thread([&]()
{
auto ff = std::make_shared<tf::Taskflow>();
ff->emplace([](){
printf("Release agent applied ;)\n");
}).release(sem);
std::condition_variable cv;
bool done = false;
std::mutex mtx;
// run called from another thread!
exec.run(*ff, [ff]() { /* captured for side-effects */});
ff.reset();
}).detach();
exec.wait_for_all();
I'd be glad to hear about any pitfalls in this approach before I implement it in a large project.
Thanks,
Michał
Tsung-Wei Huang commented
@mzient this is safe code.