问题
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.
来源:https://stackoverflow.com/questions/13381036/what-is-the-correct-use-of-process-flagpriority-level-in-erlang