Re: semaphore question



On 14 Aug 2006 12:06:45 -0700, "Jack" <junw2000@xxxxxxxxx> wrote:

Below is a simple pseudocode using semaphore:

while(1){
wait(mutex1); //LINE0

open(file_f); //LINE1
write(file_f); //LINE2

signal(mutex1);

}


In short: You can't protect file-accesses with mutexes.
Mutexes are there to protect global data (either process global or
system-wide global shared memory).

But this works only if all processes/threads obey the mutex.

--
42Bastian
Do not email to bastian42@xxxxxxxxx, it's a spam-only account :-)
Use <same-name>@monlynx.de instead !
.



Relevant Pages

  • Re: WaitForSingleObject() will not deadlock
    ... However, if you were to uncomment the line that sets x to 3, thread 2 ... don't see how I could more directly illustrate Rule, ... understand the 4 rules which were phrased in terms of mutexes and condition ... by in pthreads having never heard of a memory barrier or cache consistency. ...
    (microsoft.public.vc.mfc)
  • Re: [OT]Re: atomic flag
    ... > Ron Natalie wrote: ... > Keeping the cache in sync with the memory is done at a much lower level. ... The locking and unlocking of the mutexes consitute 'memory ... volatile bool* flag; ...
    (comp.lang.cpp)
  • Re: WaitForSingleObject() will not deadlock
    ... memory fence, too. ... Not as fast as a pthreads semaphore, ... A kernel mutex also takes care of priority ... Uncontested mutexes are extremely ...
    (microsoft.public.vc.mfc)
  • Re: [OT]Re: atomic flag
    ... >>Keeping the cache in sync with the memory is done at a much lower level. ... The locking and unlocking of the mutexes consitute 'memory ... > Unlocking the mutex flushes out cached writes. ...
    (comp.lang.cpp)
  • Re: phreads and processes
    ... It appears that in a single process, if two pthreads ask for the same shared ... memory with the same key, the first thread gets the memory, and the ... >> instead of mutexes, if I wanted to. ... > using pthreads there is usually no need to use semaphores, ...
    (comp.unix.programmer)