delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2013/06/02/18:01:55

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:message-id:date:from:mime-version:to:subject
:content-type; q=dns; s=default; b=cw5bgh0iWp8Y2WyBe1J6qXAw7rR4+
snmZjLtnD/iTVplU/SKCjN65en5gQeC2XXkavqpvCCBoZph7Xnj4Ozlcf8kOKOuI
MzojfgrCg41B2SDkRuZeNY2iyKsjbbltnnZLiCqsmfiS+tdKydbNkn5bKc7ILWZ+
nmRAANmraMS4fY=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:message-id:date:from:mime-version:to:subject
:content-type; s=default; bh=cIq0hjVtpozkV53Kqyo+9czx4/4=; b=Cz5
QRNCn0yoSm3BSQt+P/1qJWOY3RI0x2Ht/ubCYXrCqh6kp/EvSgmPmheL6gx3gu7S
BY8cgBw6pmI2iKlVhlofbZUQP3iZrKWJ6iSKaJrs6t6/33L0HSV75B9latW7irv8
CuozC3W6gcmSgK0Sgj1JNXlDzDvi+Ts0lqb+oPX0=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
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
X-Spam-SWARE-Status: No, score=-2.7 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_HOSTKARMA_NO,RCVD_IN_HOSTKARMA_YE,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.1
X-Trace: 865021224/mk-filter-2.mail.uk.tiscali.com/B2C/$THROTTLED_DYNAMIC/b2c-CUSTOMER-DYNAMIC-IP/85.210.65.166/None/drstacey AT tiscali DOT co DOT uk
X-SBRS: None
X-RemoteIP: 85.210.65.166
X-IP-MAIL-FROM: drstacey AT tiscali DOT co DOT uk
X-SMTP-AUTH:
X-Originating-Country: GB/UNITED KINGDOM
X-MUA: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130514 Thunderbird/17.0.6
X-IP-BHB: Once
X-IronPort-Anti-Spam-Filtered: true
X-IronPort-Anti-Spam-Result: ArEBAHi/q1FV0kGm/2dsb2JhbAANTYM5iRq2foNFUT0WGAMCAQIBWAgBAbBwkgSPRINCA5AAgSyHO4RihBCKNQ
Message-ID: <51ABC0BE.5080408@tiscali.co.uk>
Date: Sun, 02 Jun 2013 23:01:34 +0100
From: David Stacey <drstacey AT tiscali DOT co DOT uk>
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130514 Thunderbird/17.0.6
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: pthread_join() problem
X-Virus-Found: No

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

I'm trying to get Poco[1] working under Cygwin, and have hit a problem 
with the way it manages its threads. A short example (attached) shows 
what's going on.

Poco creates a global object that is used for managing various threads. 
In the destructor, the class calls pthread_join() to wait for the 
threads to finish. This works in Fedora (and presumably other Linux 
variants), but under Cygwin this call never returns. Note that the 
object is global, and so pthread_join() is being called after main() has 
returned and the global variables are being mopped up.

In the attached example, the problem only exists if the 
'thread_container' object is global. When run in this way, 
pthread_join() never returns and the programme locks up. If 
'thread_container' is moved local to main() then the programme works 
correctly. Sadly, I can't make such a trivial fix to the Poco code, 
which rather relies on this object being global.

The attached example (and indeed Poco) runs fine under Fedora 18 x64, 
but locks up on Cygwin (32-bit; haven't tried 64-bit). Problem exists 
with a vanilla cygwin-1.7.18-1 and the most recent snapshot (2013-06-02).

Any insight you could provide in this matter would be much appreciated - 
many thanks in advance for your help.

Dave.

[1] http://pocoproject.org/



--------------010709040408070601080808
Content-Type: text/x-c++src;
 name="testcase.cpp"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="testcase.cpp"

// Compile: g++ -o testcase testcase.cpp -lpthread
#include <unistd.h>
#include <pthread.h>
#include <iostream>
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>

// This class represents a thread.
class CThread
{
public:
	explicit CThread(const std::string message) : Message(message)
	{
		// Create a new thread.
		pthread_create(&this->ThreadID, NULL, (void*(*)(void*))&CThread::RunThread, this);
	}

	~CThread()
	{
		// Wait for the thread to exit.
		if (pthread_join(this->ThreadID, NULL))
			std::cerr << "Error waiting for '" << this->Message << "' thread to end." << std::endl;
	}

private:
	// This class should never be copied.
	CThread(const CThread&);
	CThread& operator=(const CThread&);

	// Run when the thread starts.
	// Prints out the message 5 times and exits.
	static int RunThread(void *ptr)
	{
		CThread *data = reinterpret_cast<CThread*>(ptr);
		const std::string message = data->Message + "\n";

		for (int i = 0; i < 5; ++i)
		{
			std::cout << message;
			sleep(1);
		}
		return 0;
	}

	pthread_t ThreadID;
	std::string Message;
};

typedef boost::shared_ptr<CThread> thread_ptr;
typedef std::vector<thread_ptr> thread_ptr_vector;

// This class manages a number of threads.
class CThreadContainer
{
public:
	CThreadContainer()
	{
		// Create two threads and run them.
		this->Threads.push_back(thread_ptr(new CThread("Wibble")));
		this->Threads.push_back(thread_ptr(new CThread("Wobble")));
	}

private:
	thread_ptr_vector Threads;
};

// If this variable is global, the programme locks up when run.
// If this variable is local to main() then the programme exits OK.
CThreadContainer thread_container;

int main()
{
	return 0;
}


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

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
--------------010709040408070601080808--

- Raw text -


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