Personal tools
Document Actions

lesson5b.cpp

by aullmj — last modified 2009-01-26 18:01

multithreading demo code

Click here to get the file

Size 1.0 kB - File type text/x-c++src

File contents

//companion code for lecture on threads
//Mark Aull, 2009

#include <windows.h> 
#include <stdio.h> 
#include <iostream> 
using namespace std; 

long WINAPI ThreadFunction(double lParam[]); 

void main(void) 
{
	HANDLE hThreads[3];
	double data1[2],data2[2],data3[2];
	data1[0]=1; data2[0]=2; data3[0]=3; data1[1]=15; data2[1]=10; data3[1]=5;
	unsigned long dwID; 
	hThreads[0] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunction,LPVOID(&data1),0,&dwID);
	hThreads[1] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunction,LPVOID(&data2),0,&dwID);
	hThreads[2] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunction,LPVOID(&data3),0,&dwID); 
	WaitForMultipleObjects(3,hThreads,true,1000);
	CloseHandle(hThreads[0]);
	CloseHandle(hThreads[1]);
	CloseHandle(hThreads[2]);
}

long WINAPI ThreadFunction(double lParam[])
{ 
	//for (double ii=lParam[1];ii>0;cout<<"Thread"<<lParam[0]<<": "<<ii<<endl,ii--); 
	for (double ii=lParam[1];ii>0;printf("Thread%2.0f: %2.0f",lParam[0],ii),cout<<endl,ii--);
	return 0;
} 
Related content