This is the mail archive of the
mailing list for the Cygwin project.
- From: David Stacey <drstacey at tiscali dot co dot uk>
- To: cygwin at cygwin dot com
- Date: Sun, 02 Jun 2013 23:01:34 +0100
- Subject: pthread_join() problem
I'm trying to get Poco 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.
// Compile: g++ -o testcase testcase.cpp -lpthread
// This class represents a thread.
explicit CThread(const std::string message) : Message(message)
// Create a new thread.
pthread_create(&this->ThreadID, NULL, (void*(*)(void*))&CThread::RunThread, this);
// Wait for the thread to exit.
if (pthread_join(this->ThreadID, NULL))
std::cerr << "Error waiting for '" << this->Message << "' thread to end." << std::endl;
// This class should never be copied.
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;
typedef boost::shared_ptr<CThread> thread_ptr;
typedef std::vector<thread_ptr> thread_ptr_vector;
// This class manages a number of threads.
// Create two threads and run them.
// If this variable is global, the programme locks up when run.
// If this variable is local to main() then the programme exits OK.
Problem reports: http://cygwin.com/problems.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple