thread question

closed account (zwA4jE8b)
I am starting to work with threads but do not understand the syntax of this...

_beginthread(thread, 0, (void*)12);

what exactly does (void*) do to 12? I have also seen this in other contexts, such as (char*) before some value. I understand that 12 is of type void* but do not understand what that means.

Also when I run the following code, '17' is displayed before '12', why is that?

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
#include <iostream>
#include <Windows.h>
#include <process.h>

using namespace std;

void thread( void * );

int main()
{
	cout << "Inside main" << endl;

	_beginthread(thread, 0, (void*)12);
	thread((void*)17);

	Sleep(100);

	cout << "Press ENTER to exit...";
	cin.get();
	return 0;
}

void thread( void *arg )
{
	printf("The thread() function was passed %dn", (INT_PTR)arg ) ;
}


Thanks,
Mike
(void*)12 converts integer 12 to a pointer which points to memory location 12. This is not a valid pointer, but that doesn't matter, since you're not dereferencing it anywhere. This is done because thread functions can only take a void* argument.
Normally you'd do
1
2
int i = 12;
thread(&i);
and in thread(),
1
2
int i = *(int*)arg;
printf("%d", i);
as only primitive data types can be converted to pointers directly.

Function printing 17 is in a separate thread form the one printing 12. Therefore they may happen in any chronological order. Apparently starting a thread takes some time, so 17 is printed before 12. You could move the Sleep from line 16 immediately after line 13. Then 12 would go before 17.
closed account (zwA4jE8b)
Thank you hamsterman.

So when i call thread((void*)17); I am just calling the function, not manipulating the thead.
In fact you have not posted any code that would lead us to expect that 12 would be printed at all.

The function of these void pointers in threadstart/threadstartex is that they permit you to point to any structure that contains the information you need in the new thread (as long as the thread knows what the structure is!).
closed account (zwA4jE8b)
can you give me an example Aussie. For example, how can I make the thread change the value of the declared structure x? How do I make the thread point to x?

The following code displays 2, 2.

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
#include <iostream>
#include <Windows.h>
#include <process.h>

using namespace std;

struct X
{
	X(int init){ a = init;};
	int a;
};

void thread( void * );

int main()
{
	cout << "Inside main" << endl;
	
	X x(2);

	cout << "Initialized struct x.a with the value: " << x.a << endl;

	_beginthread(thread, 0, (void*)x.a);

	Sleep(100);
	
	cout << "Press ENTER to exit...";
	cin.get();
	return 0;
}

void thread( void *arg )
{
	printf("The thread() function was passed %dn", (INT_PTR)arg );
}
Last edited on
closed account (zwA4jE8b)
What are valid values that I can pass to thread?

Can anyone point me to a good tutorial, I have not been able to find one that has any new information.
Last edited on
exiledAussie wrote:
In fact you have not posted any code that would lead us to expect that 12 would be printed at all.
How so? You might be confusing something..

CreativeMFS,
On line 13 (of your first post) you start a new thread.
On line 14 you just call a normal function.

To do something with structure X you have to pass a pointer to it. Try this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main(){
   X x(2);
   _beginthread(thread, 0, &x);//passes the address of x. Note that you don't need (void*),
   //as every pointer is convertable to a void*.
   Sleep(100);//wait for thread to execute...
   std::cout << x.a;// "5" !
   return 0;
}

void thread( void *arg ){
   X* x = (X*)arg;//convert void* to X*
   x->a = 5;
}


If you have a hard time understanding pointers, I suggest that you should leave threads for now and learn pointers first. Threads have some complex stuff apart from them.
Last edited on
closed account (zwA4jE8b)
Thank you hamsterman. I have a fairly good understanding of pointer. I have done a couple projects using dynamic arrays and also arrays of pointers. Sometimes when I see something new, like threads, it didn't click for me that I could send the address of struct obj.


So this code...
X* x = (X*)arg;
converts the void pointer to an X struct pointer, and is assigned to the X struct pointer x. Correct?
Last edited on
closed account (zwA4jE8b)
This is my finished practice code... I am wondering though, sometimes loop1 completes entirely then loop 2 runs and sometimes the outputs are mixed. Is that timing due to how fast the second thread is created? So sometimes thread1 completes before thread2 is ready?

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
#include <iostream>
#include <Windows.h>
#include <process.h>

using namespace std;

struct X
{
	X(int init){ a = init;};
	int a;
};

void thread1( void * );
void thread2( void * );

CRITICAL_SECTION sync;

int main()
{
	X x(0);
	HANDLE hs[2];
	InitializeCriticalSection(&sync);

	cout << "Inside main" << endl;
	cout << "Starting threads" << endl;

	hs[0] = (HANDLE)_beginthread(thread1, 0, &x);
	hs[1] = (HANDLE)_beginthread(thread2, 0, &x);

	WaitForMultipleObjects(2, hs, true, INFINITE);
	
	DeleteCriticalSection(&sync);

	cout << "Press ENTER to exit...";
	cin.get();
	return 0;
}

void thread1( void *arg )
{
	X *x = (X*)arg;
	for ( int i = 0; i <= 10; i++)
	{
		x->a = i;
		EnterCriticalSection(&sync);
		cout << "thread1 loop " << x->a << endl;
		LeaveCriticalSection(&sync);
	}
	return;
}

void thread2( void *arg )
{
	X *x = (X*)arg;
	for ( int i = 10; i >= 0; i--)
	{
		x->a = i;
		EnterCriticalSection(&sync);
		cout << "thread2 loop " << x->a << endl;
		LeaveCriticalSection(&sync);
	}
	return;
}
Correct?
yes
Is that timing due to how fast the second thread is created?
I suppose so
Topic archived. No new replies allowed.