1
General Discussion about just::thread / join() on Windows Vista doesn't appear to wait for the thread to finish
« on: August 28, 2009, 01:44:47 PM »
Hi!
Waiting for threads using join() on 32-bit Windows Vista platforms doesn't appear to wait for the thread to finish (it just appears to return immediately), whereas on 32-bit Windows XP platforms join() seems to work as expected.
Has anyone come across this problem before?
Here is a simple piece of code that (when compiled using Microsoft Visual C++ 2008 (Express Edition) at least) exhibits this problem:
// JoinTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <thread>
#include <mutex>
#include "windows.h"
void log(const char * const message) {
static std::mutex logMutex;
// Lock the log mutex to prevent overlapped log I/O.
std::lock_guard<std::mutex> log_lock(logMutex);
std::cout << message;
}
void log(std::string & message) {
log(message.c_str());
}
void threadCode(unsigned threadId) {
std::ostringstream message;
message << __FUNCTION__ << ": thread ID: " << threadId << " started.\n";
log(message.str());
// Wait for a while, so we can check what join() does.
Sleep(threadId * 5000L);
message << __FUNCTION__ << ": thread ID: " << threadId << " finished.\n";
log(message.str());
}
int _tmain(int argc, _TCHAR* argv[])
{
log("Main thread started.\n");
std::thread myThread1(threadCode, 1);
std::thread myThread2(threadCode, 2);
log("Main thread waiting for child thread 1...\n");
myThread1.join();
log("Main thread waiting for child thread 2...\n");
myThread2.join();
log("Main thread finished.\n");
// Wait for user input before closing-down the window.
char ch;
std::cin >> ch;
return 0;
}
On XP the child threads complete before the main thread. However, on Vista it looks like the main thread gets to the end (waiting for user input) before the child threads have even started, but if you wait for a while (i.e. up to 30 seconds) before pressing a key, the child threads do actually start and finish (it's just that the main thread isn't waiting for them).
Am I doing something wrong or is a problem/limitation of the thread library code.
Waiting for threads using join() on 32-bit Windows Vista platforms doesn't appear to wait for the thread to finish (it just appears to return immediately), whereas on 32-bit Windows XP platforms join() seems to work as expected.
Has anyone come across this problem before?
Here is a simple piece of code that (when compiled using Microsoft Visual C++ 2008 (Express Edition) at least) exhibits this problem:
// JoinTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <thread>
#include <mutex>
#include "windows.h"
void log(const char * const message) {
static std::mutex logMutex;
// Lock the log mutex to prevent overlapped log I/O.
std::lock_guard<std::mutex> log_lock(logMutex);
std::cout << message;
}
void log(std::string & message) {
log(message.c_str());
}
void threadCode(unsigned threadId) {
std::ostringstream message;
message << __FUNCTION__ << ": thread ID: " << threadId << " started.\n";
log(message.str());
// Wait for a while, so we can check what join() does.
Sleep(threadId * 5000L);
message << __FUNCTION__ << ": thread ID: " << threadId << " finished.\n";
log(message.str());
}
int _tmain(int argc, _TCHAR* argv[])
{
log("Main thread started.\n");
std::thread myThread1(threadCode, 1);
std::thread myThread2(threadCode, 2);
log("Main thread waiting for child thread 1...\n");
myThread1.join();
log("Main thread waiting for child thread 2...\n");
myThread2.join();
log("Main thread finished.\n");
// Wait for user input before closing-down the window.
char ch;
std::cin >> ch;
return 0;
}
On XP the child threads complete before the main thread. However, on Vista it looks like the main thread gets to the end (waiting for user input) before the child threads have even started, but if you wait for a while (i.e. up to 30 seconds) before pressing a key, the child threads do actually start and finish (it's just that the main thread isn't waiting for them).
Am I doing something wrong or is a problem/limitation of the thread library code.