passing vector in multi threaded application

Hello there

I am having difficulties figuring out how to pass a vector from a thread to the thread function. Its an int vector and this is how I call the thread function for it:


 
hThread = (HANDLE) _beginthreadex(NULL, 0, iterative_mergesort, (void *)&Vector1, 0, 0);



and the thread function is as follow:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
unsigned __stdcall iterative_mergesort(void *v2) {
		

	int increment;
	int left;
	int left_end;
	int right;
	int right_end;
	int current;
	int i;

	Vector tmp(v2.size());
	int size = v2.size();

	increment = 1;
         .
         .
         .


But it does not work.

I really appreciate any help in advance


MJDB wrote:
But it does not work.

That is so incredibly vague it isn't even funny.

I think you probably want to cast v2 to a Vector*, among other things.
I have tried casting it as well...

 
hThread = (HANDLE) _beginthreadex(NULL, 0, iterative_mergesort, (void *)&Vector1, 0, 0);


unsigned __stdcall iterative_mergesort(void (vector<int>* v2)) {

but still...I am a beginner in multi threading. please bare with me.

thanks alot
I think Zhuge is refering to the cast being here with your original code:
Vector tmp(v2.size());
I am using VC++. It tells me that: "v2 is undefined".

Is it possible that the problem exist when passing the vector from thread to iterative_mergesort() function?... since I am sending it as void?

thanks in advance
I'm not at a machimne with a compiler on to test this, but I don't think it's the passing to the function that is the problem, it's just how you're casting it in the sorting function, there's a capital V in your (and my copy+paste) vector tmp=that shouldn't be there.

Try simply setting size to the value obtaind from the cast void pointer without using a tmp, I think this would work?
int size = ((vector<int>*)v2)->size();
I declared Vector as vector<int>:
 
typedef vector<int> Vector;	



Then I just used in the function:
 
Vector Vector1(tl2.size());



And from that function, I am sending it to a thread:
 
hThread = (HANDLE) _beginthreadex(NULL, 0, iterative_mergesort, (void *)&Vector1, 0, 0);



This is the whole iterative_mergesort function:
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// implementing the mergesort iteratively (bottom up mergesort)
unsigned __stdcall iterative_mergesort(void* v2) {
		


	int increment;
	int left;
	int left_end;
	int right;
	int right_end;
	int current;
	int i;


	int size = ((vector<int>*)v2)->size();
	// int size = v2.size();
	vector<int> tmp(size);
	

	increment = 1;


	while (increment < size) {

		// I don't need to go over 1 for increment...so I can stop the last pass..
		if(increment == 2){ 
			return;}

		left = 0;
		right = increment;
		left_end = right - 1;
		right_end = (left_end + increment < size) ? left_end + increment : size - 1;
		current = 0;

		 while (current < size) {
			  while (left <= left_end && right <= right_end) {
					if (v2[right] < v2[left])
						  tmp[current] = v2[right++];
					else
						  tmp[current] = v2[left++];
						  current++;
				}
      
				while (right <= right_end)
						tmp[current++] = v2[right++];

				while (left <= left_end)
						tmp[current++] = v2[left++];
      
		 left = right;
		 right += increment;
		 left_end = (right - 1 < size) ? right - 1: size - 1;
		 right_end = (left_end + increment < size) ? left_end + increment : size - 1;
		}
    
		 increment *= 2;


		for (i = 0; i < size; i++)
			v2[i] = tmp[i];


		
		another_function(v2);

	 }
	
	tmp.empty();
}



you are right Moooce. The problem is not with the parameter passing because the casting works. Does it make a difference if I cast it as ((vector<int>*)v2) instead of ((Vector*)v2)?


Do I need to cast all my v2 in the iterative_mergesort() as well?

I really appreciate your help

regards
Last edited on
I'm afraid I can't compile all that code in my head so you'll have to bare with me if I get something wrong here ;-)

Also I'm no STL guru so maybe somebody else could point you in a more efficient direction, but as far as I know, like you say, you'll need to cast each v2.

The other way may be to wrap your vector in a struct and pass that as you would a normal inbuilt type without having to cast each time, if that fits your needs better?

e.g.
1
2
3
4
5
struct mydata
{
	vector<int> Vector1;
	int somethingelse;
};


and in your sort function:
1
2
	mydata *tmp = (mydata*)v2;
	int size=tmp->Vector1.size();  // << here onwards you can use the vector members accessed from tmp. 


and in your calling function (probably main())
1
2
mydata myvector;
hThread = (HANDLE) _beginthreadex(NULL, 0, iterative_mergesort, (void *)&myvector, 0, 0);


But like I said, there may be better ways, If anyone knows, there will be at least 2 people who learn something from it.
MJDB wrote:
Then I just used in the function:


Vector Vector1(tl2.size());

'Vector1' is a local variable? Then your program will crash. except you're waiting for the end of the thread but then why bother using a thread.


MJDB wrote:
Does it make a difference if I cast it as ((vector<int>*)v2) instead of ((Vector*)v2)?
No.

MJDB wrote:
Do I need to cast all my v2 in the iterative_mergesort() as well?
Yes. Plus derefence it in order to use []. example: (*((Vector*)v2))[right]

or you use v3 instead like Vector &v3 = *((Vector*)v2);
Topic archived. No new replies allowed.