Thread: Threading understanding

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    5

    Threading understanding

    I've search the net and come across _beginthread,_beginthreadex and CreateMutex which can handle the function to be threaded.

    My question is, how do use those function respectfully to be able to get the return value of the threaded function and pass a single argument which always changes?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> and come across _beginthread, _beginthreadex and Create[Thread] which can handle the function to be threaded.
    Prefer _beginthreadex over the other two - http://msdn.microsoft.com/en-us/library/kdzttdcb.aspx

    >> how ... to get the return value of the threaded function
    You can use GetExitCodeThread().

    >> and pass a single argument
    The fourth parameter to _beginthreadex() allows you to pass thread-specific parameters to your thread functions.

    >> ... which always changes
    If multiple threads are accessing the same data, and at least one thread is writing, the you'll need to synchronize access to that data - using a CRITICAL_SECTION for example.

    gg

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    5
    How do I get the return value of foo not the exit code of foo;for instance,
    my_return_datatype foo(my_argument_datatype) when being threaded?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    In Windows, threads return a DWORD and GetExitCodeThread() is how you get that value.

    If you need to "return" some other data type then you'll have to manage that yourself.

    gg

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All threads allow you to pass an extra void* parameter that services as a parameter send to your thread function. You can use it to pass data to the thread and from the thread.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding forward declarations of classes, pimpl
    By Boxknife in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2010, 01:29 AM
  2. trouble understanding the source file structure
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2006, 06:46 PM
  3. c++ threading
    By Anddos in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2005, 03:29 PM
  4. understanding recursive functions
    By houler in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 12:56 PM
  5. Problem with threading
    By osal in forum Windows Programming
    Replies: 6
    Last Post: 07-21-2004, 12:41 PM