Threads...

Started by
5 comments, last by CtrlAltDel 21 years, 6 months ago
How can I create a thread and, lets say display a simple message in a win32 enviroment, the call 2 funcitons at once?
Advertisement
you can start by visiting your local msdn.
I am kind of the slow type and msdn confuses me...
Here''s something to get you started - CreateThread. Check out the example code.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes,
SIZE_T dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadId
);


Looking at create thread, I see that you can pass values thru LPVOID lpParameter, how can I pass more than one value using create thread?

Also the following code would create two threads that would run at the same time, and so I could run 3 functions at one time?(The two process I created, along with the one that starts when the program is executed)?

hThread = CreateThread(
NULL,
0,
ThreadFunc,
&dwThrdParam,
0,
&dwThreadId);

hThread2 = CreateThread(
NULL,
0,
ThreadFunc2,
&dwThrdParam,
0,
&dwThreadId);

quote:Original post by CtrlAltDel
Looking at create thread, I see that you can pass values thru LPVOID lpParameter, how can I pass more than one value using create thread?

Pass a pointer to a structure, and populate that structure with all your input and output values. The calling thread fills-in the inputs, the worker thread fills-in the outputs.

quote:
Also the following code would create two threads that would run at the same time, and so I could run 3 functions at one time?

Yep.
quote:Original post by CtrlAltDel
Looking at CreateThread, I see that you can pass values thru LPVOID lpParameter, how can I pass more than one value using create thread?

Pack them up in a struct and pass a pointer to that struct to CreateThread[Ex].

quote:
Also the following code would create two threads that would run at the same time, and so I could run 3 functions at one time?(The two process I created, along with the one that starts when the program is executed)?

Basically, but beware of race conditions and deadlocks. You''re passing the same data to both CreateThread calls. You need to somehow synchronize access to the variable memory; see mutexes, semaphores and critical blocks.

This topic is closed to new replies.

Advertisement