Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Alexander Terekhov Newsgroups: comp.programming.threads Subject: Re: pthread reentrance issue on Visual Studio Date: Wed, 27 Jul 2011 12:39:39 +0200 Organization: A noiseless patient Spider Lines: 48 Message-ID: <4E2FEAEB.957D7CAB@web.de> References: Reply-To: terekhov@web.de Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Info: mx04.eternal-september.org; posting-host="MCbSEzW5qwGLFMNXJh39IQ"; logging-data="19422"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18wKikVvdToVJpLhMQyYWedO9GwYqkqtIs=" X-Mailer: Mozilla 4.77 [en] (Windows NT 5.0; U) X-Accept-Language: en Cancel-Lock: sha1:XVycoF4j35ZibIKJ2fuhTauZwPo= Xref: x330-a1.tempe.blueboxinc.net comp.programming.threads:479 BlackLight wrote: > > I am currently attempting to use pthreads for a C++ project under > Visual Studio 2005 in a Windows environment. I am currently using > pthreadVCE2.lib library for my purposes. The things went quite > flawlessly so far, but now, because of some more complex requirements > for my application, I need some more intensive multithreaded design, > and this also implies to have some mutex lock attempts on the same > mutex from the same thread which already got that mutex (since the > same function may be called by different threads, I cannot know > whether that thread already acquired that mutex or not). Here I got > the issue, with my program looking as deadlocked. After a whole day > spent debugging my mutexes in the application, I simply tried to run > the following code: > > #include > #include > > int main() > { > pthread_mutex_t mutex; > pthread_mutex_init (&mutex, NULL); > > pthread_mutex_lock (&mutex); > std::cout << "Got lock 1\n"; > pthread_mutex_lock (&mutex); > std::cout << "Got lock 2\n"; > pthread_mutex_unlock (&mutex); > std::cout << "Released lock 1\n"; > pthread_mutex_unlock (&mutex); > std::cout << "Released lock 2\n"; > > return 0; > } > > And, surprise, this code deadlocks as well on "Got lock 1" (while, in > the same moment, the same code runs flawlessly on my Cygwin > environment using gcc). My question is: is it an issue with pthread > library, with the version I'm using, or with my Visual Studio > project's options? How to get everything correctly reentrant? Unless you don't want to redesign your code to avoid the need for recursive locking, use PTHREAD_MUTEX_RECURSIVE and non-null attr parameter for pthread_mutex_init(), see pthread_mutexattr_settype(). regards, alexander.