[1.7] pthread_mutex_lock/pthread_mutex_unlock strange blocking problem

Thomas Stalder cygwinml@gmail.com
Tue Jun 2 14:11:00 GMT 2009


Hello,

Sometimes pthread_mutex_lock ou pthread_mutex_unlock block.

In order to reproduce the problem, I have made a test application
(test.c) who always block

$ gcc test.c -o test
$ ./test
thread id=1
after pthread_create 1
after pthread_create 2
thread id=2
thread id=3
after pthread_create 3
after pthread_create 4
thread id=4
thread id=5
after pthread_create 5
thread id=6
after pthread_create 6
after pthread_create 7

The application can create 1,2,3 threads or many more before blocking.

--
Thomas
-------------- next part --------------
#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h>
#include <unistd.h>


static pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t my_mutex2 = PTHREAD_MUTEX_INITIALIZER;
int nb_thread=0;

void *thread_func(void *parm)
{
	int id;
	pthread_mutex_lock(&my_mutex);
	nb_thread++;
	id = nb_thread;
	pthread_mutex_unlock(&my_mutex);
	
	pthread_mutex_lock(&my_mutex2);
	printf("thread id=%d\n", id);
	pthread_mutex_unlock(&my_mutex2);

	pthread_exit(0);
	return NULL;
}

int main(void)
{
	int tmp_nb=0;
	while(1)
	{
		int err = 0;
		pthread_attr_t  attr;
		pthread_t thread_id;

		/*  Initialize the attribute */
		err = pthread_attr_init(&attr);
		if (err)
		{
			printf("pthread_attr_init err=%d\n", err);
		}
		err = pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
		if (err)
		{
			printf("pthread_attr_setinheritsched error\n");
		}

		err = pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
		if (err)
		{
			printf("pthread_attr_setdetachstate error\n");
		}

		/*  Create the thread with our attribute */
		
		err = pthread_create(&thread_id, &attr, thread_func, NULL);
		if (err)
		{
			printf("pthread_create error\n");
		}	
		pthread_mutex_lock(&my_mutex2);
		printf("after pthread_create %d\n", ++tmp_nb);
		pthread_mutex_unlock(&my_mutex2);
		pthread_attr_destroy(&attr);
	}
}
-------------- next part --------------
--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


More information about the Cygwin mailing list