Let me make sure I understand C pthread mutex -
I have a global variable Is that correct? I do not plan on doing this if I want to see Where do you 1 - 2 - 3 - correct. Now, if you write that loop, <3p> As Ben said in his comment, you want to use the condition variable for signaling (note :) where do you lock () Will and Unlock () Mute X In the form of a thumb of the rule, to reduce the dispute, keep the lock in brief. This lock can be as easy as + reading + unlock, but if the sequence of operations depends on the fact that the flag should not be changed, keep the lock as necessary as possible. flag , a function
it () , And a function
that () . Now,
main () ,
this () and
that () are all opportunities where they need to read and / or write the flag. Is . To use a mute x properly I'll need it:
pthread_mutex_t and start it.
(this) and
that () and
pthread_join () because it's me
To wait for the main
to finish on and
before exiting it () .
main () ,
it () , or
that () , where < Code> flag reading or writing, call
pthread_mutex_lock () then [read / write]
flag then call
pthread_mutex_unlock () < / Code>
flag in an infinite loop in :
while (flag! = Value) continues;
lock () and
unlock () mutex and other threads with the
flag Code> main () running continuously on it?
flag must be updateable atomically, and The other core needs to see those changes. Not all architectures are guaranteed). Even if this works, it is still a busy wait loop, and it should not be used.
Comments
Post a Comment