just::thread Support Forum
September 09, 2010, 10:50:13 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: just::thread V1.4.1 for Windows and Linux Released
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: join() on Windows Vista doesn't appear to wait for the thread to finish  (Read 1850 times)
JohnWS
Newbie
Posts: 6


View Profile
« 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.
Logged
Anthony Williams
Administrator
Newbie
Posts: 46



View Profile WWW
« Reply #1 on: August 30, 2009, 09:06:56 PM »

This is strange. I'll investigate and get back to you.

Logged
Anthony Williams
Administrator
Newbie
Posts: 46



View Profile WWW
« 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.
Logged
Anthony Williams
Administrator
Newbie
Posts: 46



View Profile WWW
« 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.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!