How to make Processes Run Parallel in Erlang?

Posted by Ankit S on Stack Overflow See other posts from Stack Overflow or by Ankit S
Published on 2010-04-21T07:06:47Z Indexed on 2010/04/21 7:23 UTC
Read the original article Hit count: 434

Filed under:
|
|

Hello,

startTrains() ->
TotalDist = 100,
Trains = [trainA,trainB ],
PID = spawn(fun() -> 
        train(1,length(Trains)) end),
[ PID ! {self(),TrainData,TotalDist} || TrainData <- Trains],
receive
{_From, Mesg} ->
    error_logger:info_msg("~n Mesg ~p ~n",[Mesg])
        after 10500 ->
    refresh
end.

so, I created Two Processes named trainA, trainB. I want to increment these process by 5 till it gets 100. I made different processes to make each of the train (process) increments its position parallely. But I was surprised to get the output sequentially i.e process trainA ends then process trainB starts. But I want to increment themselves at simultaneously. I want to run processes like this


trainA 10 trainB 0
trainA 15 trainB 5
....
trainA 100 trainB 100 

but I m getting


trainA 0
....
trainA 90
trainA 95
trainA 100
trainA ends

trainB 0
trainB 5
trainB 10
.....
trainB 100

How to make the processes run parallel/simultaneously? Hope you get my Q's. Please help me.

© Stack Overflow or respective owner

Related posts about erlang

Related posts about processes