hi all,
I need help in threading can any body help me
thanks in advance.
if interested then please welcome for code snippest
hi all,
I need help in threading can any body help me
thanks in advance.
if interested then please welcome for code snippest
Win32 question, moved.
But I recommend you search the web. If you don't ask a specific question, you won't get an answer.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
what specifically do you need help with? starting threads? understandign the concept of threads? or something more specific?
To start a thread use the Win32 API command CreateThread like this:
your thread function should look like this:Code:HANDLE hThread; DWORD ThreadId; DWORD MyThreadData; hThread = CreateThread(NULL , 0 , &MyThreadFunc , &MyThreadData , 0 , &ThreadId);
The NULL is for a security attributes structure pointer, nto somehting you want to mess with if you are just starting out. The first 0 is for the stack size, it has to be 0 for default size, the secodn 0 is for creation method, 0 starts the thread immediately, CREATE_SUSPENDED creates it in a suspended state until you executeCode:DWORD WINAPI MyThreadFunc(LPVOID lpMyThreadData){ // your thread code goes here return 0; }
Code:ResumeThread(hThread);
Last edited by abachler; 05-02-2007 at 09:18 AM.
Supposedly, _beginthreadex is better/safer than CreateThread (maybe someone can remind me why again?).
examples
_beginthreadex
1) Executes per-thread initialization for the MS CRT.
2) Wraps the thread procedure so that it ends the thread with _exitthreadex instead of ExitThread, which performs per-thread cleanup for the MS CRT.
The second point is actually more important, because the functions that need it will do the initialization themselves if necessary. However, if _exitthreadex is not called, the application will leak resources.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Threads created with CreateThread implicitly call ExitThread when returning, so sayeth the documentation, so sayeth the flock.
The other drawback (for some applications) is that threads started with beginthreadex must use the __stdcall calling convention, which can cause incompatibilities with some 3rd party development suites that only support the __cdecl style.
So, my .02 is to know how to use both when one may have an advantage over the other, but CreateThread is a lot easier to use if you are just learning to write multithreaded code, such as the poster.
Last edited by abachler; 05-02-2007 at 09:30 AM.
Huh? The CreateThread's start routine must use __stdcall just as the _beginthreadex one. Furthermore, no Win32 compiler in existence does not support __stdcall - since WinMain and window procedures must use this convention, and all WinAPI functions do, not supporting it would pretty much disqualify the compiler from being "Win32".
Also, you imply that CreateThread implicitly calling ExitThread is a drawback for _beginthreadex. I fail to see how.
The rule of thumb is simple: use _beginthreadex if you use any part of the C or C++ standard library, CreateThread otherwise. Since you use the standard library for a few things almost implicitly in C++, just always use _beginthreadex.
Better yet, use Boost.Thread.
And of course, don't use any way of creating a thread unless you really understand the multitude of issues with multithreading: resource contention, race conditions, deadlocks, cache coherency, etc.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
No, just that the argument that 'failing to call ExitThread when exiting a thread created with CreateThread fails to deallocate resources' being a PLUS for beginthreadex, is in fact false. Since other than an application meltdown all threads created with CreateThread call ExitThread whether you write it into your code or not.Also, you imply that CreateThread implicitly calling ExitThread is a drawback for _beginthreadex. I fail to see how.
The resource leak comes from failing to call _endthreadex. I worded that poorly.
Point is, if you use any of errno, strtok, or a host of other functions in a thread that doesn't exit with _endthreadex (as it automatically does if _beginthreadex was used to start it) then there will be a resource leak. Therefore, you should use _beginthreadex to ensure that _endthreadex is, in fact, called.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law