Thread: Multi threading examples please

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You may not have a need for it, and that's fine. But please refrain from pushing your ideals upon others in the C++ forum. This is about standard, portable C++.
    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.

  2. #17
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Elysia View Post
    You may not have a need for it, and that's fine. But please refrain from pushing your ideals upon others in the C++ forum. This is about standard, portable C++.
    In this case the OP was asking for examples of multi-threading using Windows API, which was the key point of the examples. I grabbed what I already had as working examples. If the OP or anyone else actually expressed interest in this, I would take the time to convert the example to proper C++, but as it stands now, it doesn't seem anyone cares.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If anyone uses Win32 API over standard C++, you should be skeptical and investigate if the OP really needs it or if the OP has been misled or simply does not know better.
    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.

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    So basically you don't think threads are platform specific?

  5. #20
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Elysia View Post
    Win32 API over standard C++
    As mentioned in my prior posts and in the example code, Windows API includes WaitForMultipleObjects(), which is used in the GetNode function to wait for both a mutex and a semaphore with a non zero count, then decrement the non-zero semaphore count, with a single "atomic" call to the operating system. This simplifies the code and eliminates any issues related to thread priority. Also as mentioned in prior posts, most of the operating systems I've worked with going back to the 1970's have something similar to WaitForMultipleObjects(), but there's no native equivalent for POSIX, or std::thread.

  6. #21
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by whiteflags View Post
    So basically you don't think threads are platform specific?
    Not anymore with C++11, they aren't.

    That being said, you should now typically almost always prefer task-based parallelism to anything else. This includes the use of std::async and the resulting std::future. After that, std::thread should be your next goto. Using platform-specific threading APIs is now a bad idea. I darn near pooped myself the first time I got threaded code to compile on both Windows and Linux and it's all thanks to C++11.

  7. #22
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by whiteflags View Post
    So basically you don't think threads are platform specific?
    Quote Originally Posted by MutantJohn View Post
    Not anymore with C++11, they aren't.
    The OP is working on a game, which is probably using other Windows specific stuff, such as directx for graphics and controller input . There's still the issue of lack of support for the equivalent of Windows (or XBOX) WaitForMultipleObjects with POSIX or std::thread.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by rcgldr View Post
    As mentioned in my prior posts and in the example code, Windows API includes WaitForMultipleObjects(), which is used in the GetNode function to wait for both a mutex and a semaphore with a non zero count, then decrement the non-zero semaphore count, with a single "atomic" call to the operating system. This simplifies the code and eliminates any issues related to thread priority. Also as mentioned in prior posts, most of the operating systems I've worked with going back to the 1970's have something similar to WaitForMultipleObjects(), but there's no native equivalent for POSIX, or std::thread.
    Semaphores are (counted) mutexes. There is a function that grabs multiple locks at the same time. You can make your own WaitForSingleObject/WaitForMultipleObjects class/functions in C++. It's not a perfect 1:1 replacement, but it's still better than nothing and you're not missing out on functionality just because you use it.

    Quote Originally Posted by rcgldr View Post
    The OP is working on a game, which is probably using other Windows specific stuff, such as directx for graphics and controller input . There's still the issue of lack of support for the equivalent of Windows (or XBOX) WaitForMultipleObjects with POSIX or std::thread.
    So? There's nothing saying that can't mix the C++ standard library and other windows-specific stuff such as directx.
    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.

  9. #24
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    WaitForMultipleObjects
    I'm actually not sure what this is but it sounds like either joining on a series of threads or waiting on multiple asynchronous values to resolve. If if it's threads, then it's just simple join calls with a condition variable.

  10. #25
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by rcgldr View Post
    WaitForMultipleObjects
    Quote Originally Posted by MutantJohn View Post
    I'm actually not sure what this is but it sounds like either joining on a series of threads or waiting on multiple asynchronous values to resolve.
    The caller can specify to wait for any or for all of the objects to be signaled. In the example I posted, a mutex is used for ownership of a message queue (linked list), and a semaphore for the count of messages in the queue. A single "atomic" call with WaitForMultipleObjects() allows a thread to pend for both the mutex and a non-zero semaphore count, then decrements the semaphore count, and returns back to the previously pending thread. This eliminates any issues related to thread priority.

    In the multi-threaded operating systems and applications I've worked with going back to the 1970's (mainframes and mini-computers), the primary method of communication between threads was message queues, but there was also an event for exception handling (like shutdown), and a timer, so the equivalent of WaitForMultipleObjects was used, waiting for any of the objects to be signaled: message, event, or timer, to be signaled. The same feature can be found in many of the RTOS implementations for embedded devices.

  11. #26
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    It seems like it can almost be expressed with condition variables nowadays. C++11 brought more than just std::thread.

    Going back to the OP's question though... Have they even posted since? I think we all got distracted by the very C-like code XD

    The Windows API seems kind of cool though. I was reading up on WaitForMultipleObjects on the MSDN. As much as it pains me to say this, M$ has some pretty good docs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi threading examples please
    By kingnoger in forum C++ Programming
    Replies: 1
    Last Post: 12-23-2016, 02:18 AM
  2. Using Multi-threading and/or Multi-process
    By lehe in forum C++ Programming
    Replies: 5
    Last Post: 07-14-2009, 11:43 AM
  3. Is Multi-threading necessary?
    By keira in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-14-2007, 05:13 AM
  4. Multi-threading
    By justgotthis in forum Linux Programming
    Replies: 7
    Last Post: 10-08-2005, 10:28 AM

Tags for this Thread