Hi Luarance,

I believe the difference is down to pthreads not allowing two locks on
the same mutex from a single thread, while the windows equivilant
allows this - this means the standard Windows mutex is re-entrant,
whereas the pthread one isn't.   To get the same behaviour as windows
under linux you have to use the OpenThreads::ReentrantMutex.  You'd
use this in code that sets a lock and the recurses, such a case would
be an osgPlugin that uses a RenentrantMutex to serialize access to it,
but the mutex doesn't prevent it calling itself - a conventional Mutex
under pthreads would lock in this situation.

The Windows mutex does prevent locks from different threads being
acquired multiple times, so if you modified your code to test this it
should work the same under Windows and Pthreads/unix.

Robert.





On Wed, Feb 4, 2009 at 1:53 PM, Laurence Muller <l.y.l.mul...@uva.nl> wrote:
> Hi,
>
> It seems that there is a difference in behavior when using the
> OpenThreads::Mutex class in Linux and Windows.
>
> For our (crossplatform) application it is required to synchronize data
> between two different threads. In order to prevent a race condition, we used
> the OpenThreads mutex lock (OpenSceneGraph 2.6.1).
> To test the behavior of the mutex lock, I created a small application
> (attached below).
>
> Our Linux machine produces the following output:
> [laure...@fedora mutex_test]$ ./main
> test 1 (trylock): ok
> -- application blocked --
>
> Using the same application under windows (compiled with mingw):
> main.exe
> test 1 (trylock): ok
> test 2 (lock): ok
> test 3 (lock again): ok
> test 4 (unlock): ok
> -- application finished --
>
> The Linux version shows the correct behavior, the Windows version seems to
> ignore the lock calls and does not block at all.
>
> Questions:
> 1. Are additional settings required to use the OpenThreads mutex lock under
> windows?
> 2. What alternatives are available for the OpenThreads mutex lock? (Manually
> implement calls using pthread?)
>
> Kind Regards,
> - Laurence
>
> ---------
> // main.cpp
> // g++ main.cpp -o main -I /home/userx/osg/include -L /home/userx/osg/lib
> -lOpenThreads
>
> #include <stdlib.h>
> #include <iostream>
> #include <OpenThreads/Mutex>
> using namespace std;
>
> OpenThreads::Mutex *mt_sync;
>
> int main(int argc, char **argv)
> {
>   mt_sync = new OpenThreads::Mutex();
>
>   printf("test 1 (trylock): ");
>   if(mt_sync->trylock() == 0)
>       printf("ok\n");
>   else
>       printf("fail\n");
>
>   printf("test 2 (lock): ");
>   if(mt_sync->lock() == 0)
>       printf("ok\n");
>   else
>       printf("fail\n");
>
>   printf("test 3 (lock again): ");
>   if(mt_sync->lock() == 0)
>       printf("ok\n");
>   else
>       printf("fail\n");
>
>   printf("test 4 (unlock): ");
>   if(mt_sync->unlock() == 0)
>       printf("ok\n");
>   else
>       printf("fail\n");
>
>   delete mt_sync;
>
>   return 0;
> }
>
>
> _______________________________________________
> osg-users mailing list
> osg-users@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to