Haych
Haych

Reputation: 942

Wake a process from sleeping - Erlang

Is it possible to wake a process externally in Erlang after it has been sent to sleep for infinity?

I would like to wake it from a different process which does hold the process ID of the process which is asleep.

I used this within the process which I want to sleep:

timer:sleep(infinity)

If it is not possible to wake it externally, what other options are available to me?

Upvotes: 4

Views: 395

Answers (2)

BlackMamba
BlackMamba

Reputation: 10252

From the source code of this function:

-spec sleep(Time) -> 'ok' when
  Time :: timeout().
sleep(T) ->
    receive
    after T -> ok
    end.

you can find that sleep just let the process wait, do nothing.

if you want wake the process, you need send message to the sleeping process and do receive.

Upvotes: 1

Steve Vinoski
Steve Vinoski

Reputation: 20024

Rather than using timer:sleep/1, put the process into a receive so that it waits for a message. When the other process wants it to proceed, it can simply send it a message. Assuming the message matches what the receive is looking for, the first process will then exit the receive and continue.

Upvotes: 8

Related Questions