Func = fun(Arg) ->
Parent = self(),
Pid = spawn( fun() -> timer:sleep(1000), Parent ! {self(), Arg + 2} end ),
receive {Pid, Val} -> Val
end,
RetVal = Func(),
SomeOtherValue = 10,
% rest of the code
io:format( "~p~n", [RetVal] ),
% more code here
In the above code, calling Func() block the execution until receive loop within Func is done receiving the data from the spawned process. SomeOtherValue is assigned only after the execution of Func.
What I want is not that. I want the execution to continue even if the execution of Func is not finished. I want to block only when I am trying to use RetVal variable.
Currently I am acheiving this by waiting for the response from the spawned process only when I need the data from the process, i.e., same as blocking when the variable is accessed.
Func = fun(Arg) ->
Parent = self(),
Pid = spawn( fun() -> timer:sleep(1000), Parent ! {self(), Arg + 2} end ),
end,
Pid = Func(),
SomeOtherValue = 10,
% rest of the code
RetVal = receive {self(), Val} -> Val end,
io:format( "~p~n", [RetVal] ),
% more code here
Even though it is a trivial code change, I would like if the parallel assignment is allowed and block when I try to use it (if necessary).
No comments:
Post a Comment