Results 1 to 2 of 2

Thread: Multiple arguments to worker thread.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241

    Multiple arguments to worker thread.

    Im looking for an example of how to pass multiple arguments to a worker thread.

    thx in advance.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    struct THREADPARAMS
    {
      int iParam;
      void *pParam;
      long lParam;
    };
    
    DWORD ThreadProc(void *p)
    {
      THREADPARAMS *pars = (THREADPARAMS)p;
      pars->iParam...
      // ...
      delete pars;
    }
    
    void func()
    {
      HANDLE hThread;
      THREADPARAMS *pars = new THREADPARAMS;
      pars->iParam = 3;
      pars->pParam = &something;
      pars->lParam = 2348329;
      hThread = CreateThread(ThreadProc, pars);
    }
    The way this is written there is one limitation: once CreateThread is called the creating thread may not access pars anymore in any way since it can' t know when the other thread finishes and deletes the parameters. If you still need the access you must think if a different way to avoid a leak.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width