delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/08/25/19:41:54

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-ID: <430E5510.7060401@embedded.cl>
Date: Thu, 25 Aug 2005 19:32:32 -0400
From: Pablo Bleyer Kocik <pbk AT embedded DOT cl>
User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716)
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Problems with pthread mutex attributes

--------------090208020406020403080902
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit


  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

--------------090208020406020403080902
Content-Type: text/plain;
 name="testThread3.cc"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="testThread3.cc"

#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;
}


--------------090208020406020403080902
Content-Type: text/plain; charset=us-ascii

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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019