CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2003
    Location
    PA
    Posts
    124

    syntax for thread that returns a value

    Ok, I need to create a thread function uses the comm port that is passed a single parameter (char[16]), and it is to return a char * (LPSTR) value.

    I already have the comm port functionality completed and this works fine as a single threaded app. I need to port it to a multi-threaded app.

    I looked at all of the documentation that I can find, but cannot get a grip on how to get a value back from the thread function.

    CreateThread() is passed the function name and params (a struct of type LPVOID, another problem area), but the return value from the CreateThread is the thread ID. How can:
    1. Pass the char[16] param to the thread
    2. get the LPSTR value returned from the thread comm function.
    3. This value MUST be returned prior to procedding with further processing. I am contemplating the use of WaitForSingleObject(...) but am unsure of the downside of it.

    An example would be tremendously helpful.

    Thanks in advance...
    Last edited by rick7423; June 2nd, 2005 at 09:35 AM.
    There are 10 types of people in the world, those that understand binary and those that don't.
    Using VS 2010

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: syntax for thread that returns a value

    All threads run in the same memory space. So, just create a structure to pass into the new thread that contains the char[16] you want to pass in, and a member for the return value from the thread. Then, when the thread is done, signal the parent thread, so you can read the value back.

    Code:
    Pseudo code:
    
    struct MyData
    {
      char m_Input[16];
      std::string m_Output;
    };
    
    ...
    MyData *pData = new MyData;
    // initialize data
    // Create thread, passing 'pData'
    ...
    // You can use Events to wait for the thread to finish...
    ...
    std::cout << "Thread returned:  " << pData->m_Output << std::endl;
    Viggy

  3. #3
    Join Date
    Jul 2003
    Location
    PA
    Posts
    124

    Re: syntax for thread that returns a value

    Outstanding Mr. Viggy, I think that gave me the push I needed.
    There are 10 types of people in the world, those that understand binary and those that don't.
    Using VS 2010

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: syntax for thread that returns a value

    NP. And Whew! I just kinda threw that together. I was hoping you'd get the gist of it!



    Incidentally, if you're using MFC or a thread with a message pump, then you can "signal" your main thread using a user defined message, and the ::PostMessage function.

    Viggy

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640

    Re: syntax for thread that returns a value

    I looked at those links, Andreas, but they don't really address passing and using data between threads. However, they are a good read, if you are just starting out with threads!



    Viggy

  7. #7
    Join Date
    Jul 2003
    Location
    PA
    Posts
    124

    Re: syntax for thread that returns a value

    I also read these and also other threads referenced from MSDN before posting here. I was still confused on a couple issues, but I think MrViggy has pushed me in the right direction. I just need to hack out some working code now.
    There are 10 types of people in the world, those that understand binary and those that don't.
    Using VS 2010

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: syntax for thread that returns a value

    Quote Originally Posted by MrViggy
    I looked at those links, Andreas, but they don't really address passing and using data between threads. However, they are a good read, if you are just starting out with threads!
    Well..actually they do at the lowest level...the first one passed the 'this' pointer to the thread....the last one the handle in order to post messages...at least as far as I remember.

    Nevertheless, they should only serve as a starter...I usually assume that people get the trick by looking at them....since passing a pointer to a structure is basically the same than any other pointer...such as the 'this' one...

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

Featured