Unix to Windows Porting Dictionary for HPC

RSS

Links

Function List

pthread_join


Unix

header file: pthread.h

int pthread_join(pthread_t thread, void **value_ptr);
      

Windows

header file: Windows.h

DWORD WINAPI WaitForSingleObject(
  __in  HANDLE hHandle,
  __in  DWORD dwMilliseconds
);
      

Purpose

The pthread_join function is used to synchronize a main thread with the termination of a child thread. If the value_ptr is not null, the exit code from the child will be returned in that location.

Discussion

The Windows equivalent function is effectively the same as the POSIX version. The function returns the child termination code as a DWORD. The Windows call has the added ability of a timed wait. If the value of dwMilliseconds is zero, the parent thread will not wait. If the value is Infinite the effect will be the same as the Unix call. If a parent thread wants to wait for the termination of multiple children, you can use WaitForMultipleObjects instead.

Example of Use in Windows

#include "stdafx.h"
#include <time.h>
#include <windows.h>
#include <iostream>

using namespace System;
using namespace std;

HANDLE hEvent;

DWORD WINAPI ChildThread(LPVOID iValue)
{
cout<<”Hello from the Child Thread”<<endl;

return 0;
}

int main()
{
HANDLE hCThread;
DWORD dwGenericThread;

hCThread = CreateThread(NULL,0,ChildThread,NULL,0,NULL);

cout<<"Waiting for the thread to complete.."<<endl;

WaitForSingleObject(hCThread,INFINITE);

cout<<"Thread Completed."<<endl ;

CloseHandle(hCThread);

return 0;
}

    
blog comments powered by Disqus
Valid HTML 4.01 Transitional Valid CSS!