TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Phil Crown
from: Jonathan de Boyne Pollard
date: 1996-11-09 09:04:36
subject: Mutex semaphores

PC>
  > ULONG  someFn()
  > {
  >   ULONG rc = 0;
  >   requestMutexSem();
  >   rc = something;
  >   releaseMutexSem();
  >
  >   return rc;  // A calling thread is pre-empted here before returning rc.
  >
  >               // A second threads comes along and completes the
  >               // function before the first thread has finished,
  >               // thus causing rc to contain invalid data.
  >               // Is this possible?
  > }
PC>

  Yes; if, say, a second higher-priority thread were blocked at the start
  of the same function, it could be unblocked by the release of the
  semaphore, and run the whole function before the first thread were run
  again.

  However, whether this makes `rc' invalid is dependent from the nature of
  the function.  Good multithreaded design involves working under the
  assumption that all information gained during an atomic operation may go
  out of date immediately, because of the actions of other, concurrent,
  threads.

  If this causes problems with code based upon the results obtained, then
  your granularity is too fine, and should be coarsened.  For example,
  assume that `someFn' above were returning whether a list was empty or
  not, that the calling code were

    if (someFn())                           // List is empty
        delete the_list, the_list = 0 ;     // So destroy the list

  and than it was not desired that a non-empty list be destroyed.

  In this case, the entire `if' statement should be an atomic operation,
  not just the test function, otherwise another thread could add elements
  to the list in between the time that the test function exits its mutex
  section and the time that the first thread actually destroys the list.

  > JdeBP <
___
 X MegaMail 2.10 #0:

--- Maximus/2 3.01
* Origin: DoNoR/2,Woking UK (44-1483-725167) (2:440/4)
SEEN-BY: 50/99 270/101 620/243 625/160 711/401 409 410 413 430 808 809 934
SEEN-BY: 711/955 712/407 515 624 628 713/317 800/1
@PATH: 440/4 141/209 270/101 712/515 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.