Thread: Need help in Threading in win32

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    2

    Question Need help in Threading in win32

    hi all,
    I need help in threading can any body help me
    thanks in advance.
    if interested then please welcome for code snippest

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    what specifically do you need help with? starting threads? understandign the concept of threads? or something more specific?

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    To start a thread use the Win32 API command CreateThread like this:

    Code:
    HANDLE hThread;
    DWORD ThreadId;
    DWORD MyThreadData;
    
    hThread = CreateThread(NULL , 0 , &MyThreadFunc , &MyThreadData , 0 , &ThreadId);
    your thread function should look like this:

    Code:
    DWORD WINAPI MyThreadFunc(LPVOID lpMyThreadData){
    
    // your thread code goes here
    
    return 0;
    }
    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 execute
    Code:
    ResumeThread(hThread);
    Last edited by abachler; 05-02-2007 at 09:18 AM.

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Supposedly, _beginthreadex is better/safer than CreateThread (maybe someone can remind me why again?).
    examples

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    _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

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    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.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Also, you imply that CreateThread implicitly calling ExitThread is a drawback for _beginthreadex. I fail to see how.
    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.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 API or Win32 SDK?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-20-2005, 03:26 PM
  2. Win32 Thread Object Model Revisted
    By Codeplug in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2004, 08:50 AM
  3. Win32 Threading
    By X PaYnE X in forum Windows Programming
    Replies: 5
    Last Post: 02-21-2004, 07:43 PM
  4. OLE Clipboard :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 08-11-2002, 05:57 PM
  5. Win32 API Tutorials?
    By c++_n00b in forum C++ Programming
    Replies: 9
    Last Post: 05-09-2002, 03:51 PM