CreateThread() function question

This is a discussion on CreateThread() function question within the C++ Programming forums, part of the General Programming Boards category; So just by looking at the msdn document of the following kernal32 function, Code: HANDLE WINAPI CreateThread( __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    34

    CreateThread() function question

    So just by looking at the msdn document of the following kernal32 function,

    Code:
    HANDLE WINAPI CreateThread(
      __in_opt   LPSECURITY_ATTRIBUTES lpThreadAttributes,
      __in       SIZE_T dwStackSize,
      __in       LPTHREAD_START_ROUTINE lpStartAddress,
      __in_opt   LPVOID lpParameter,
      __in       DWORD dwCreationFlags,
      __out_opt  LPDWORD lpThreadId
    );
    lpStartAddress [in]
    A pointer to the application-defined function to be executed by the thread. This pointer represents the starting address of the thread. For more information on the thread function, see ThreadProc.
    I am not sure whether the function specified by the argument should be defined within the same .cpp file where I call the CreateThread() from. Right now, within the .cpp file that I call the CreateThread() function, I included a header file that has the declaration of the function and the definition of the function is in a .obj file that I am linking my program against.

    Thanks!

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    There is no reason the function needs to be defined in the same .cpp file, or for that matter the same module (DLL) as the call to CreateThread().
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    However, consider also this page:
    _beginthread, _beginthreadex (CRT)

    To sum up the blathering on that page, if you use the CRT, use __beginthreadex instead of CreateThread, or you will leak memory in some special circumstances.
    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

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    Awesome, thank you guys

    Just one more quick question regarding thread programming. After creating the thread, is it generally recommended to use WaitForSingleObject() to wait for the thread to finish the function, instead of polling with GetExitCodeThread() (by checking if it returns STILL_ACTIVE)? I am doing the latter, but while I was reading some WiX(Windows Installer wrapper module) implementations, I realized that they take the formal approach.
    Last edited by chiefmonkey; 05-14-2009 at 05:57 PM.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Use one of the Wait functions on the thread handle. Don't poll if you don' t have to.

    gg

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    I'll put it in stronger words: never poll unless there's really no other way.

    It's also recommended that after creating the thread, you continue doing work instead of doing anything that waits for the thread to finish, since that would pretty much defeat the point. You only wait for a thread when you really need the result right there.
    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. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21