Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » WaitForMultipleObjects() analog?
WaitForMultipleObjects() analog? [message #14920] Thu, 20 March 2008 00:53 Go to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Is there one in U++? Or is there technique to wait for a number of *events*, awaiking on one of them?
Re: WaitForMultipleObjects() analog? [message #14966 is a reply to message #14920] Sun, 23 March 2008 08:36 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
No, AFAIK there is no unified way how to do that in Linux. (There is solution for sockets AFAIK, but not that nice general solution as in Win32).

Mirek
Re: WaitForMultipleObjects() analog? [message #15460 is a reply to message #14966] Fri, 25 April 2008 03:35 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

OK, who knows how to wait for multiple Semaphores in Linux? Sometimes it is very useful to awake on one of objects from list. All the complex applications usually wait for a number of objects in it`s threads (i.e. i/o event and quit event).
Are there any experienced Linux coders, how do you solve this problem? Maybe we will integrate it into U++ (if it`s not already).

For now I can`t write extensive portable applications under U++ mostly due to many MT unsupported features - like WaitForMultipleObjects. I believe this thing will be handy.

P.S. I found Linux` version of WaitForMultipleObjects function. May be it will be useful.

[Updated on: Fri, 25 April 2008 03:38]

Report message to a moderator

Re: WaitForMultipleObjects() analog? [message #16106 is a reply to message #15460] Wed, 28 May 2008 10:16 Go to previous messageGo to next message
hojtsy is currently offline  hojtsy
Messages: 241
Registered: January 2006
Location: Budapest, Hungary
Experienced Member
Mindtraveller,

What are you trying to achieve with multiple Semaphores? I would expect that an alternative implementation could use Monitors. These are fancy mutexes which also have Wait and Pulse methods.
I am working on a U++ example code which uses Monitors to implement a Producer-Consumer queue.

See the concept explained in http://www.albahari.com/threading/part4.html

Hojtsy
Re: WaitForMultipleObjects() analog? [message #16135 is a reply to message #16106] Wed, 28 May 2008 22:19 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

hojtsy wrote on Wed, 28 May 2008 12:16

Mindtraveller,

What are you trying to achieve with multiple Semaphores? I would expect that an alternative implementation could use Monitors. These are fancy mutexes which also have Wait and Pulse methods.
I am working on a U++ example code which uses Monitors to implement a Producer-Consumer queue.

See the concept explained in http://www.albahari.com/threading/part4.html

Hojtsy

I just didn`t fully understand about Monitors usage as we don`t have ones in U++ for now.
Then about waiting a number of Semaphores. Let`s imagine the real world application. Your program must heavily work with a number of devices attached to serial port (or ethernet or something else). Of course it is solved by adding a class which creates new thread and have cycle which is sleeping most of time. But there is a number of awake signal types. And these signals could be activated from within: this thread, OS and main thread. These signals are:
  1. application close event. sleeping i/o thread should do finalization and close.
  2. OS i/o event. sleeping thread should handle the fact of newly sent or received data.
  3. queue event. sleeping thread just got a new task to do. this task was added by another thread.
  4. timer event (timeout). happens when system i/o functions didn`t respond in time or final value of some deferred OS call didn`t arive in time.

These events are dramatically different from each other and when my thread is awakened I should know the type of this event to handle it. So I use one synchronization object (let`s say Semaphore for simplicity) for each event type. Doing this with one Semaphore would be a nightmare, wouldn`t it?
Re: WaitForMultipleObjects() analog? [message #16149 is a reply to message #16135] Thu, 29 May 2008 15:12 Go to previous messageGo to next message
hojtsy is currently offline  hojtsy
Messages: 241
Registered: January 2006
Location: Budapest, Hungary
Experienced Member
Mindtraveller,

AFAIK unix can only block on multiple conditions inside "select".
Anything else is emulation. The code you attached can indeed wait on multiple Semaphores, but not on file descriptors which you also need. That is the more interesting part I think.

Implementation alternative A:

You can block on a single Semaphore or Monitor, which protects a message queue. In this case any of the events you list should be an other thread putting a message to the queue, which the worker thread awakes to process. The direct IO communication (the select call) should be done on a different thread which puts incoming messages to the message queue and signals the Semaphore/Monitor.
In this case the problem arises how do you cancel the IO thread which is blocking inside select when you want to shutdown? You have three options: either call shutdown on the file description it is waiting for, or have it select on a pipe too which only gets written to when a shutdown occurs, or have the thread automatically wake up every 0,1 sec from the select and poll for the shutdown flag. In all cases the IO thread gets waken up from select and can exit. The worker thread could just wake up on a shutdown message arriving in it's message queue.

Implementation alternative B:

You can block inside a select which waits for the IO port and multiple pipes, each of which communicates different events. Timeout is automatically handled by select, shutdown and queue events could be other threads writing some bytes to the dedicated pipes, which also wakes up the thread doing the select.

I think solution A is better because it separates the low level pipe hacking from the main logic of the application, needs less pipes, and also makes the main part portable without preprocessor guards.
Re: WaitForMultipleObjects() analog? [message #16153 is a reply to message #16149] Thu, 29 May 2008 20:32 Go to previous message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Agree. So we should have everything needed for such a tricky multithreading inside U++.
For now we have Mutex, Semaphore and no pipes.

[Updated on: Thu, 29 May 2008 20:33]

Report message to a moderator

Previous Topic: Interprocess communication with U++
Next Topic: sMutexLock implementation
Goto Forum:
  


Current Time: Fri Mar 29 02:30:08 CET 2024

Total time taken to generate the page: 0.01174 seconds