Multi-threading (Process.h) in a class

Hello guys, I've recently started doing some multi-threading with C++ and Visual Studio 2010.

I really don't even know where to begin with this problematic code, I have written a function with the header demanded by the function _beginthreadex(), in a class... Now, I've realized there is something terribly wrong with what I'm doing because I think it will need to be re-written. Good thing I'm just playing with it for now!

I would love to know why I can't do this, and how I'm supposed to do it. Then some reassurance about C++ standard library threading stuff being added.

Here it is, I've stopped in the middle of this small exercise because it's obvious I'm doing something wrong...

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
class BrutePrimes
{
private:
	volatile std::vector<int> prime_list;

	unsigned __stdcall is_prime( void *iPrime )
	{
		int *end = reinterpret_cast<int*>(iPrime);

		for( int i = 2; i < *end; i++ )
		{
			if( !((*end)%i) )
				return 1;
		}
		prime_list.push_back(*end);
		return 0;
	}
public:

	void AddPrimes(int a, int b)
	{
		for( int i = a; i<b; i++ )
		{
			_beginthreadex( NULL, 0, is_prime, &i, 0, NULL );
			i++;
			_beginthreadex( NULL, 0, is_prime, &i, 0, NULL );
		}
	}
};
1. The multiple calls to _beginthreadex withing the loop is redundant. The code is equivalent to:
1
2
3
4
5
6
7
	void AddPrimes(int a, int b)
	{
		for( int i = a; i<b; i++ )
		{
			_beginthreadex( NULL, 0, is_prime, &i, 0, NULL );
		}
	}


2. As you create more and more threads, the OS spends more and more processing time just scheduling threads. There comes a point (a surprisingly low point too) where it's unproductive to create more threads. To this end, thread pools are used. The idea behind a thread pool is that there's a fixed pool of threads that can be given tasks. So the pool has a queue of tasks to be run. The threads don't stop when the task is complete, they just become available to run more tasks.

3. You don't serialise access to prime_list. As such it's almost guaranteed to be corrupted as different threads attempt to update it at the same time. We call this a race condition. Making it volatile doesn't fix this.

4. You pass i by reference, but continue to increment it. So the value of *end in each thread isn't the same as i passed to the thread.
Last edited on
The actual semantics of it don't even matter to me, I want to know how to even use these functions inside of a class first of all (compile).

Oh, and I was going to ask about some good reading for multi-threaded programming w/ C++.

Re: 3. ...
What exactly does volatile do there?
and I was going to ask about some good reading for multi-threaded programming w/ C++.
Start by uinderstanding multithreaded issues. http://lmgtfy.com/?q=C+multithreaded+programming

What exactly does volatile do there?
Nothing.

You use volatile to tell the compiler about variables that can be modified thru means that the compiler cannot detect, it tells the compiler not to cache that variable in a register.
The actual semantics of it don't even matter to me, I want to know how to even use these functions inside of a class first of all (compile).
Topic archived. No new replies allowed.