Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #478 > unrolled thread

pthread reentrance issue on Visual Studio

Started byBlackLight <blacklight86@gmail.com>
First post2011-07-27 01:42 -0700
Last post2011-07-27 03:57 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.programming.threads


Contents

  pthread reentrance issue on Visual Studio BlackLight <blacklight86@gmail.com> - 2011-07-27 01:42 -0700
    Re: pthread reentrance issue on Visual Studio Alexander Terekhov <terekhov@web.de> - 2011-07-27 12:39 +0200
    Re: pthread reentrance issue on Visual Studio David Schwartz <davids@webmaster.com> - 2011-07-27 03:57 -0700

#478 — pthread reentrance issue on Visual Studio

FromBlackLight <blacklight86@gmail.com>
Date2011-07-27 01:42 -0700
Subjectpthread reentrance issue on Visual Studio
Message-ID<ed5076fb-1240-4c2c-8da8-b6592ded1ce8@s2g2000vbw.googlegroups.com>
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 <iostream>
#include <pthread.h>

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?

Thanks,
Fabio

[toc] | [next] | [standalone]


#479

FromAlexander Terekhov <terekhov@web.de>
Date2011-07-27 12:39 +0200
Message-ID<4E2FEAEB.957D7CAB@web.de>
In reply to#478
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 <iostream>
> #include <pthread.h>
> 
> 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.

[toc] | [prev] | [next] | [standalone]


#480

FromDavid Schwartz <davids@webmaster.com>
Date2011-07-27 03:57 -0700
Message-ID<373e2415-cfdd-42f1-95cd-0a092c5c831c@t8g2000prm.googlegroups.com>
In reply to#478
On Jul 27, 1:42 am, BlackLight <blackligh...@gmail.com> wrote:

> 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?

You asked your thread to wait for itself, so that's what it did.

"Doctor, it hurts when I do that."
"Well then don't do that."

DS

[toc] | [prev] | [standalone]


Back to top | Article view | comp.programming.threads


csiph-web