Thread: Threads...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Threads...

    Hey,

    I've got a typical thread (DWORD WINAPI threadname) running from CreateThread() (I used the tutorial in the FAQ).

    However, I honestly don't see the purpose of threading if the thread can't communicate with the main program. How can I get the value of what the thread returns, or, what a thread sets?

    Any help will be greatly appreciated.

    Thanks in advance,

    Guitarist809
    ~guitarist809~

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    At least once, read MSDN docs and hundreds of MS samples using CreateThread().
    (KB , MSDN articles, PSDK, etc...)
    + the Richter'

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by Alex31 View Post
    At least once, read MSDN docs and hundreds of MS samples using CreateThread().
    (KB , MSDN articles, PSDK, etc...)
    Thanks for the response!

    I've actually read a bunch of samples, however, none of them (at least from what I saw) were actually capable of returning a value after the thread is finished. There was only the ability to pass the thread a parameter of type LPVOID, which really does nothing for me.
    ~guitarist809~

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can get the "return value" from the thread by using the API GetExitCodeThread.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>There was only the ability to pass the thread a parameter of type LPVOID, which really does nothing for me.

    You can use the LPVOID to pass a pointer to a structure which you share between the main thread and worker thread. The worker thread can write to variables in the shared struct, while the main thread reads from it. You'll need thread synchronization to prevent bad things from happening; for example, pass an Event in the structure, so that the thread can signal the main program when it's finished processing a certain segment of data, and the main program can then read the result from another member of the structure.

    Et cetera.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by Hunter2 View Post
    >>There was only the ability to pass the thread a parameter of type LPVOID, which really does nothing for me.

    You can use the LPVOID to pass a pointer to a structure which you share between the main thread and worker thread. The worker thread can write to variables in the shared struct, while the main thread reads from it. You'll need thread synchronization to prevent bad things from happening; for example, pass an Event in the structure, so that the thread can signal the main program when it's finished processing a certain segment of data, and the main program can then read the result from another member of the structure.

    Et cetera.
    Oh... That explains quite a lot. I'll do a quick google for synchronization.

    Quote Originally Posted by matsp
    You can get the "return value" from the thread by using the API GetExitCodeThread.
    If the above way fails, I'll give this a try.

    Thanks for the help everyone =]
    ~guitarist809~

  7. #7
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    _beginthreadex() is a safer win32 threading approach.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>_beginthreadex() is a safer win32 threading approach.
    Just to elaborate, the C runtime library (CRT) requires some initialization/cleanup in order to work properly in threads that you create. _beginthreadex will ensure that this occurs, and then call CreateThread() for you.

    Also to note: The method I mentioned is just one possible way. For example, globals are also shared across threads, although it's frowned upon to use globals in the first place (and you still need synchronization when doing any shared access).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by Hunter2 View Post
    >>_beginthreadex() is a safer win32 threading approach.
    Just to elaborate, the C runtime library (CRT) requires some initialization/cleanup in order to work properly in threads that you create. _beginthreadex will ensure that this occurs, and then call CreateThread() for you.

    Also to note: The method I mentioned is just one possible way. For example, globals are also shared across threads, although it's frowned upon to use globals in the first place (and you still need synchronization when doing any shared access).
    Yea, that's what I did at first, but I think it's a pretty bad idea to use them, so I decided to switch my methods of getting stuff done
    ~guitarist809~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  3. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM
  4. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM