pass several arguments to LPVOID param of CreateThread

This is a discussion on pass several arguments to LPVOID param of CreateThread within the Windows Programming forums, part of the Platform Specific Boards category; What is the best way to pass more than one argument to the LPVOID parameter of CreateThread -- No MFC? ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    39

    pass several arguments to LPVOID param of CreateThread

    What is the best way to pass more than one argument to the LPVOID parameter of CreateThread -- No MFC? (I am actually using _beginthreadex, which is virtually identical.) I, of course, am assuming it is possible.

    I have tried everything but only get compiler errors.

    If it isn't possible, how else can I pass arguments to a thread before it starts?

    thanks

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    You have a few options....

    Make all variables global - this is faster and less trouble, but should be discouraged as it can lead to problems and its best to have variables local where you can...

    or

    Define a struct or class with all the data you need as members.......you can then pass a pointer through the function and collect it at the other end.....

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    39
    Thanks, I will try passing a structure.....great idea!

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Oh....one thing.....be carefull when you are doing this with threads.....remember, 2 threads accessing/amending the same memory at the same time can cause diasters.........you might need some sort of thread sync....

    Also, remember that passing a pointer to a tread from another thread is only viable if the data stays in scope.....if thread1 passes a pointer to local data to thread2, and then goes out of scope....you've had it!

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    You should create a global structure. Pass a reference or pointer of the structure to the worker thread.

    For example:

    -----
    // this should be global

    UINT YourWorkerThreadFunc(LPVOID pParam);

    typedef struct tagYOURWORKERTHREAD
    {
    ... // whatever you want to pass into the worker thread
    ...
    ...
    } YOURWORKERTHREAD;
    ----

    ----
    YOURWORKERTHREAD *ptp = new YOURWORKERTHREAD;

    ptp->blah = whatever
    ...

    CWinThread *m_pYourWorkerThread = AfxBeginThread(YourWorkerThreadFunc, ptp, THREAD_PRIORITY_NORMAL,
    0, CREATE_SUSPENDED);

    m_pYourWorkerThread ->m_bAutoDelete = FALSE;

    // You need this to close the worker thread later

    m_pYourWorkerThread = m_pYourWorkerThread ->m_hThread;
    m_pYourWorkerThread ->ResumeThread();
    -----


    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass Param out in Activex
    By Duyetnv in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2008, 05:17 AM
  2. How can I pass arguments through a GUI (win32)?
    By bikr692002 in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2006, 05:17 PM
  3. how do i pass arguments to a function^^^please help
    By adzza69 in forum C++ Programming
    Replies: 3
    Last Post: 08-26-2004, 06:03 PM
  4. passing arguments
    By Baard in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2003, 07:10 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 01:11 PM

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