What is the correct use of process_flag(priority,

2019-06-04 14:57发布

问题:

I want to use the process_flag(priority, Level) to set the priority of a process. I am a little confused as to where it should go in my code and can't seem to find an example.

The two options that I can see are:

(1) Set the flag before spawning the process:

process_flag(priority, max),
register(myprocess, spawn(fun() -> myprocess() end)),

(2) Set the flag within the function after it has been spawned:

myprocess() ->
process_flag(priority, max),
%do stuff

Also, if option 1 is correct, do I need to reset the flag to normal before spawning other processes?

回答1:

Option 2 is the correct one. As the documentation says, process_flag/2 "sets certain flags for the process which calls this function". I don't think any of the process flags are inherited to spawned processes.

The documentation also suggests not using the max priority level:

The max priority level is reserved for internal use in the Erlang runtime system, and should not be used by others.