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?
(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.
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!).
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?
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.
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?
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?