CreateThread and parameters?

This is a discussion on CreateThread and parameters? within the Windows Programming forums, part of the Platform Specific Boards category; Is there a way to pass multiple parameters to the thread, instead of using global values? Thank you....

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    CreateThread and parameters?

    Is there a way to pass multiple parameters to the thread, instead of using global values?


    Thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Create a struct containing all the information you want to pass.

    Create an instance of that struct(*), fill it with information, and then pass a pointer to the whole thing to the thread.

    (*) you have to make sure that the scope of the memory allocated lasts at least as long as the thread being created. For example, pointers to local variables almost never work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    (*) you have to make sure that the scope of the memory allocated lasts at least as long as the thread being created. For example, pointers to local variables almost never work.
    Can you please give me an example...
    Last edited by Devil Panther; 12-02-2006 at 12:38 PM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Yeah, use malloc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Ok, but how can I be sure they don't get mixed between different threads?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Because each thing returned by malloc is guaranteed to be a unique object which will last until you explicitly call free.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Devil Panther
    Ok, but how can I be sure they don't get mixed between different threads?
    use different mallocs for different threads...

    PS. And as I remember it is not recomended to call the CreateThread directly
    use beginthreadex - it wraps the windows call and provides some additional initialization needed for correct work of the C/C++ runtime libraries
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Here's what MSDN says about the issue.

    A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multi-threaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.
    Last edited by Ken Fitlike; 12-03-2006 at 04:48 AM. Reason: use [quote][/quote] tags for text to ensure it wraps

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  9. #9
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by Salem
    Because each thing returned by malloc is guaranteed to be a unique object which will last until you explicitly call free.
    I'm not sure this will work for me.
    Here is what I'm trying to do:

    I'm scanning a directory for it's files.
    Once the file is found I would like to open a new thread to process it.


    How can I use malloc if the name of the variable is the same?
    Where should I allocate the memory, inside the new thread?


    thanks.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Something like that maybe:
    Code:
    while(...)
    {
       int* a = malloc(sizeof *a);
       long h;
       if(a)
       {
    	unsigned thread_id;
    	*a = rand(); //fill with something useful
    	h = _beginthreadex(NULL, 0, thread_proc, (void*)a, 0, &thread_id);
    	if(h != 0)
    		CloseHandle((HANDLE)h); //we not interested in the return value of the thread
       }
    }
    
    
    unsigned  __stdcall thread_proc( void * param)
    {
      int value = 0;
      if(param)
    	  value = *(int*)param;
      
      //do processing
      free(param);
      return 0;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Oh... Using the address of the newly allocated variable.
    Got it, thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  12. #12
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    [strike]vart's example may kill the thread before it gets a chance to run (and free memory).[/strike](See below) (beginthread also returns uintptr_t.)

    Also, pthreads are your friend.

    *a = rand(); //fill with something useful
    That makes the example worth it...
    Last edited by Cactus_Hugger; 12-03-2006 at 11:33 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    vart's example may kill the thread
    Yeah? And how exactly?

    MSDN says:
    Closing a thread handle does not terminate the associated thread.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    /me retracts he previous statement as steathily as possible.
    That seems illogical, but MSDN does say it... my mistake. (CBoard needs strikeout.)

    Now you see why pthreads are my friend. All this nonsense between beginthread, beginthreadex, and family is driving me nuts.
    Last edited by Cactus_Hugger; 12-03-2006 at 11:34 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  15. #15
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    It is interesting (at least for me) to note that you can easily pass this as the parameter, and connect the thread back to an object.

    Code:
        m_hThread = ::CreateThread( 0, 0, 
            /* ( DWORD ( WINAPI * )( LPVOID ) ) */ run, 
            LPVOID(this), 0, &m_dwThreadID );
    }
    
    /* static */ DWORD WINAPI foo::run( LPVOID vthis )
    {
        foo * fthis = reinterpret_cast< foo * >( vthis );
    
        return fthis->run();
    }
    
    DWORD foo::run( void )
    {
           // Do yr stuff with foo
    }
    Oh yeah, MSDN says this about CloseHandle and threads.

    >> Closing a thread handle does not terminate the associated thread. To remove a thread object, you must terminate the thread, then close all handles to the thread.
    Last edited by Tonto; 12-03-2006 at 11:41 PM.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Errors for CreateThread..
    By gopi_tony in forum Windows Programming
    Replies: 20
    Last Post: 10-22-2006, 09:58 AM

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