Classes and threading

I'm having a go at some multithreading with an operation that is quite slow, but can work well concurrently.
My question is, is this the best/only way to make a slow function that operates with an object work with multiple threads?
Are there any ways my slow function void GetMap() can be a member function, or at least be called as a member function of WebMap or must it be a global function to become threadable?

Any ideas welcome and thanks....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#define MAP_UNSET		0
#define MAP_READY		1
#define MAP_COMPLETE	2
#define NUMBER_OF_THREADS	2

class WebMap
{
	// Some member vars
public:
	WebMap(){ Done=MAP_UNSET; }
	void MapSet(char* fn,int xo,int yo)
	{
		// Store the supplied values to member vars.
		....
		Done=MAP_READY;
	}
};

void GetMap(LPVOID ldata)
{
WebMap *A=(WebMap*)ldata;
	A->some WebMap member function or variable.
	A->Done=MAP_COMPLETE;
	_endthread();
}

WebMap ThreadData[NUMBER_OF_THREADS+1];

int main()
{
	....
	// Setup the data for each instance
	ThreadData[0].MapSet("SomeName1",0,0);
	ThreadData[1].MapSet("SomeName2",1,0);

	// begin each GetMap() in its own thread
	_beginthread(GetMap,0,&ThreadData[0]);
	_beginthread(GetMap,0,&ThreadData[1]);

	do{
		// Something pretty happens to keep the user occupied here
	}while(ThreadData[0].Done + ThreadData[1].Done < NUMBER_OF_THREADS*MAP_COMPLETE);
	....
}
Last edited on
Unless there's a yielding function (e.g. Sleep()) in the do while loop, you're using three threads at max capacity, not two, which means each of the threads that are doing useful work will only reach 66% efficiency if there's only two CPUs on the system.

Instead of depending on flags to know when the threads have ended, you can just wait for them to, which doesn't take any CPU time. The function that does this is typically called "join", although it may be called something different in the interface you're using.
Thanks for the tips helios, this is all quite new for me. I did a bit of searching for join (I'm using the Windows API) and found WaitForSingleObject( hThread, TimeOut ); which appears to be what I need for this.
I do have a Sleep() in the main loop, but since each thread uses about 5% CPU and takes around 4-5sec to complete I assume this is OK. Looks like the WaitForSingleObject will do the work of the Sleep() with a bit more funtionality just fine for the main loop.
Any ideas on calling the function though? Is it impossable to have the threaded function actually be a member funtion of class WebMap rather than it being a global?
It can be a static member function, but you won't find a threading interface that lets you start threads with non-static member functions. If you want, you can pass the object pointer in the parameter struct and then use it to call the non-static function.

EDIT: Come to think of it, Boost may let you start threads with non-static member functions.
Last edited on
Topic archived. No new replies allowed.