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]

pthread_join() problem


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/


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

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

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