Multiprocessing and y4mdenoise

"Steven M. Schultz" <[email protected]> Tue, 26 May 2009 20:04:44 -0700 (PDT)
Newsgroups gmane.comp.video.mjpeg.devel
Message-ID <[email protected]>
	It's been a while since I've had reason to (re)post the following.  THe "P" in
	pthreads does not mean "Portable" - there are dragons lurking when writing
	threaded code.

        With linux a mutex can, according to the manpages, be of type 'fast',
        'recursive' or 'error checking'.  The 'fast' type has no concept of
        thread "ownership" and any thread can unlock any mutex *even if it is
        not the thread that locked it".   On the other hand the 'error checking'
        type does enforce mutex "ownership" and only the thread that locked
        a mutex may unlock it, attempting to unlock a mutex not owned by the
        thread returns an error (EPERM).  Thus checking the status returned
	by pthread_mutex_lock() and pthread_mutex_unlock() IS A GOOD IDEA.

        FreeBSD, Solaris and BSD/OS all use 'error checking' mutexes (I did
        verify this with a simple test program).  The default linux mutex
        attribute is, for some unknown reason,  'fast'.

        As you can imagine a program that expects 'fast' mutexes will not
        behave correctly on a system which uses 'error checking' mutexes (the
        reverse case might actually work - I haven't tried it ;)).

        The workaround is to override the default with:

---------
#ifdef PTHREAD_MUTEX_ERRORCHECK
	pthread_mutexattr_t mu_attr;
	pthread_mutexattr_t *p_attr = &mu_attr;

	pthread_mutexattr_init(&mu_attr);
	pthread_mutexattr_settype( &mu_attr, PTHREAD_MUTEX_ERRORCHECK );
#else
	pthread_mutexattr_t *p_attr = NULL;
#endif
	pthread_mutex_init( &frame_buffer_lock, p_attr );
---------

	A long time ago I requested that Andrew add such code to mpeg2enc when it
	was being threaded and it did catch a couple portability errors in mutex logic.

	So I strongly urge any threaded coded, y4mdenoise for example, be as portable
	as possible and create mutexes as shown above.    There's also the code in
	mpeg2enc to use as a guide.

	Cheers,
	Steven Schultz

------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT 
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, & 
iPhoneDevCamp as they present alongside digital heavyweights like Barbarian 
Group, R/GA, & Big Spaceship. http://p.sf.net/sfu/creativitycat-com