reference to gateway_t from async_node

reference to gateway_t from async_node

In the following code snippet copied from, I have a question about the validity of function parameter gateway.

How can I guarantee that it is safe to access gateway in the following thread function. My concern is that if flow graph gets reclaimed on exception, I think it becomes unsafe to access gateway and asyncThread does not know that.


void run(int input, gateway_t& gateway) {


asyncThread = std::thread{

[&,input]() {

std::cout << "World! Input: " << input << '\n';

int output = input + 1;








3 posts / 0 new

Best Reply

Hi chialun,

Of course it is unsafe to access any variable after it has undergone destruction. Thus, the question is not specific to the flow graph and boils down to how to make sure the variable, which is created on the first thread, still exists while being accessed on the second thread. 

The obvious solution is to use heap memory instead of the stack memory for holding the graph along with its nodes, so once the exception is thrown necessary variables are not destroyed. However, the disadvantage of this is to track now when to delete that heap memory.


gateway.reserve_wait() prolongs graph::wait_for_all until gateway.release_wait() is called even in a case of exception and cancellation of the graph execution. In other words, if an exception is occurred, graph will wait for gateway.release_wait() in any case. I.e. it is safe to access gateway if you can guarantee that it is reserved with the reserve_wait method.

Leave a Comment

Please sign in to add a comment. Not a member? Join today