Skip to main content
gamedev.net
🔒 Locked

Threads (Windows/Crossplatform)

Started by AntonyCoder 5 replies 500+ views
Original Post
AntonyCoder
AntonyCoder
Hi, I want to add threading to my game, so I can do FM2006 style processing in the background.(it's a footy manager too) but I'm not sure about a few things. What's the best approach to using threads? Should I use native system calls for each platform(No idea how different they are?) or should I use a multi-system application like booast? (Please bear in mind I'd like to port to ps3 if it does have the promised linux kit with it) For now I have threads working fine using the winapi, but I'm wonder what to do to avoid data clashing, should I use mutexes? (And if so, my own, or the supplied?) or are there better techniques? I'm using C++ 2005 Express Free edition with the windows platform sdk if that matters. Also, I've heard that Visual C++ adds it's own thread stuff, including a thread keyword to make copies of all globals to avoid data clashes. Is this advised, or is it slow and better to use locking where needed with good design? Thanks.
AntonyCoder
AntonyCoder
Also, is there any function to determine if a thread is still running, or do I need to create my own variable that is set to true before starting, and then set to false by the thread just before it exits?
Or is there a better method?

-Edit- Also having a little trouble actually getting any data to clash when using multiple threads on the same target.

Here's a quick test I wrote.

// ThreadTest.cpp : Defines the entry point for the console application.//#include "stdafx.h"// Thread spawner for HANDLE makeThreadValue( int (*function),LPVOID *value){HANDLE threadHandle;	threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)function,value, 0, NULL);	return threadHandle;};HANDLE makeThread(int (*function)){	HANDLE threadHandle;	threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)function,NULL, 0, NULL);	return threadHandle;};void AlterThreadPriority( HANDLE threadHandle,int priority){	switch(priority){	case 1:		priority = THREAD_PRIORITY_IDLE;		break;	case 2:		priority = THREAD_PRIORITY_LOWEST;		break;	case 3:		priority = THREAD_PRIORITY_BELOW_NORMAL;		break;	case 4:		priority = THREAD_PRIORITY_NORMAL;		break;	case 5:		priority = THREAD_PRIORITY_ABOVE_NORMAL;	case 6:		priority = THREAD_PRIORITY_HIGHEST;		break;	case 7:		priority = THREAD_PRIORITY_TIME_CRITICAL;		break;	}	SetThreadPriority( threadHandle,priority);}void haltThread( HANDLE threadHandle ){	SuspendThread( threadHandle );}void restartThread(HANDLE threadHandle){	ResumeThread( threadHandle);}void waitForObject(HANDLE threadHandle,int wait){	if(wait>0){	WaitForSingleObject( threadHandle,wait);	}else{	WaitForSingleObject( threadHandle,INFINITE);	}}void closeObject( HANDLE in){	CloseHandle( in );};void cleanThread( HANDLE threadHandle ){	TerminateThread( threadHandle ,0 );}void threadActive( HANDLE threadHandle ){	}HANDLE makeMutex(){	return CreateMutex(NULL, 0, NULL);}void cleanMutex( HANDLE mutexHandle){	ReleaseMutex( mutexHandle );}int te,ty,tu;DWORD WINAPI test( LPVOID dat){	printf("Thread in.\n");	while(1)	{		ty = te * 40 * tu;		te++;		te = ty + te - ty;		printf("TE:%d \n",te);	}	return 0;}class Thread{public:	Thread( int (*function),void *dat = NULL )	{		_func = function;		_dat = dat;	}	void SetData( void * dat )	{		_dat = dat;	}	void Run()	{		_thread = makeThreadValue( _func,(LPVOID *)_dat );	}	void Resume()	{		restartThread( _thread );			}	void Halt()	{		haltThread( _thread );	}	HANDLE _thread;	void *_dat;	int (*_func);};int _tmain(int argc, _TCHAR* argv[]){	printf("Dum de da.\n");	Thread * t1 = new Thread( (int *)test );	Thread * t2 = new Thread( (int *)test );	t1->Run();	t2->Run();	int a = 0;	int c=0;	while(true)	{		a++;		int b = 25*2343*3645/23423+2354*2343*634*454+3454/6;		if(a == 100000000)		{						printf("Dum.\n");			a=0;		}	}	return 0;}


Yet despite two threads without any data locks operating on the same variables, produces no invalid data or crashes? Is it one of those things you code for cos it could happen eventually, but it doesn't happen all the time?

daerid
daerid
If using the CRT, make sure you use _beginthreadex and _endthreadex instead of the pure WIN32 API functions.
daerid@gmail.com
RDragon1
RDragon1
Try boost::thread. Super elegant, C++-friendly, cross-platform.
Nitage
Nitage
www.google.com
'Boost'
I'm feeling lucky
joanusdmentia
joanusdmentia
You know, just typing www.boost.org would have been much shorter [smile]
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.