Author Topic: join() on Windows Vista doesn't appear to wait for the thread to finish  (Read 9832 times)

JohnWS

  • Newbie
  • *
  • Posts: 6
    • View Profile
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.

Anthony Williams

  • Administrator
  • Full Member
  • *****
  • Posts: 103
    • View Profile
    • just::thread C++ Thread Library
Re: join() on Windows Vista doesn't appear to wait for the thread to finish
« Reply #1 on: August 30, 2009, 09:06:56 PM »
This is strange. I'll investigate and get back to you.


Anthony Williams

  • Administrator
  • Full Member
  • *****
  • Posts: 103
    • View Profile
    • just::thread C++ Thread Library
Re: join() on Windows Vista doesn't appear to wait for the thread to finish
« Reply #2 on: September 02, 2009, 02:12:51 PM »
I can confirm that this does happen on Vista, but not on XP. I'll post an update when the problem has been fixed.

Anthony Williams

  • Administrator
  • Full Member
  • *****
  • Posts: 103
    • View Profile
    • just::thread C++ Thread Library
Re: join() on Windows Vista doesn't appear to wait for the thread to finish
« Reply #3 on: September 04, 2009, 12:26:43 PM »
This has now been fixed. All existing users should have received an email with an upgrade link. If anyone doesn't get an email, let me know and I'll send it to you again.