Google
  Web www.spinics.net

[Openh323-devel] Bug : PRandom::Number() is not thread-safe with VC++ compiler.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


Dear OpenH323 developers,

With VC++ compiler ( and some other compilers), the static object at block scope is not thread-safe. 

http://blogs.msdn.com/oldnewthing/archive/2004/03/08/85901.aspx


Therefore, the following method is not thread-safe.

// pwlib/src/ptclib/random.cxx

unsigned PRandom::Number()
{
#ifndef P_RTEMS
  static PMutex mutex;  //  <-- not thread-safe
#endif
  PWaitAndSignal wait(mutex); 

  static PRandom rand;
  return rand;
}

Here is a "error" scenario :

   a. First thread comes and try to initialize the static object "PMutex mutex" but not finish this task.

   b. Second thread comes and thinks that the static object PMutex mutex was initialize. Therefore, it excecutes "PWaitAndSignal wait(mutex)" : bug will happen because the mutex is not completely  initialized.

Suggestion : move "static PMutex mutex;" into file scope.

Thank for reading.



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Openh323-devel mailing list
Openh323-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/openh323-devel

[Open H.323]     [IETF SIP]     [Gnu Gatekeeper]     [Asterisk PBX]     [Fedora Linux]     [Gimp]     [Yosemite News]     [Yosemite Photos]     [Yosemite Campsites]     [ISDN Cause Codes]

Add to Google Powered by Linux