This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Problems with pthread mutex attributes



Hello list.


I am having problems using pthread conditional variables with mutexes created using mutex attributes with the latest Cygwin. If I initialize my mutexes with NULL attributes to get the default behavior, everything works. However if I try to use mutex attributes my program freezes.

I am attaching a small test set written in C++ I devised to exhibit the problem.

Has anyone faced the same trouble? Best regards.

--
PabloBleyerKocik /"The use of COBOL cripples the mind; its teaching
 pablo          / should, therefore, be regarded as a criminal
  @bleyer.org  / offense." -- Edsger Wybe Dijkstra
#include <pthread.h>
#include <string.h>
#include <iostream>

static pthread_mutex_t mutex;
static pthread_cond_t cond;

static bool quit = false;

void *
runner(void *p) {
	struct timespec tv;
	tv.tv_sec = 3;
	tv.tv_nsec = 0;

	std::cout << "Runner running..." << std::endl;
	nanosleep(&tv, NULL);
	std::cout << "Runner returning..." << std::endl;

	pthread_mutex_lock(&mutex);
	quit = true;
	pthread_mutex_unlock(&mutex);
	pthread_cond_broadcast(&cond);

	return NULL;
}

void *
waiter(void *p) {
	std::cout << "Waiter " << (int)p << " running..." << std::endl;
	pthread_mutex_lock(&mutex);
	while (!quit) pthread_cond_wait(&cond, &mutex);
	pthread_mutex_unlock(&mutex);
	std::cout << "Waiter " << (int)p << " returning..." << std::endl;

	return NULL;
}


int
main(int argc, char **argv) {
	pthread_t d, w1, w2;

#if 1 // Program freezes
	pthread_mutexattr_t a;
	pthread_mutexattr_init(&a);
	pthread_mutexattr_settype(&a, PTHREAD_MUTEX_NORMAL);

	pthread_mutex_init(&mutex, &a);
	pthread_mutexattr_destroy(&a);
#else // This works
	pthread_mutex_init(&mutex, NULL);
#endif

	pthread_cond_init(&cond, NULL);

	pthread_create(&d, NULL, runner, NULL);

	pthread_create(&w1, NULL, waiter, (void *)1);
	pthread_create(&w2, NULL, waiter, (void *)2);

	pthread_join(d, NULL);
	pthread_join(w1, NULL);
	pthread_join(w2, NULL);

	return 0;
}

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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]