40media waiting for 2 different events in a single thread


waiting for 2 different events in a single thread



REMOVED - reason: not really needed.

my questions are:

  • can I use a linux UDP socket from two different threads? answer was here
  • I have two different events I would like to wait for using just one thread. One of such events is the addition of an element to a stack and another is the availability of data on a socket.
    I can use a boost::condition_variable.wait(lock) for the stack and boost::asio::io_service for the socket. But there is no mechanism (that I am aware of) that allows me to wait for both events at the same time (polling is out of the question). Or is it?
  • Is there any other alternative solution for this problem that I'm not aware of? - I'll figure this one out by myself.



Program to open large MySQL dumps

1:



Finding an available network port on the machine

New Answer

But there is no mechanism (that I am aware of) that allows me to wait for both events at the same time (polling is out of the question).


Monitor file in Java on Linux 64bits
Or is it?.
Preventing multiple process instances on Linux
Not that I'm aware of, and not without polling...


Control Debug Level in C++ Library - Linux
you'll need a thread to wait for each asynchronous event.


Is there a difference calling daemon(0,0) from within a program, and launching a program to be in background and redirecting it's output
You can use a blocking stack or like you said use boost::condition_variable which blocks until there is something on the stack.


Reporting library for Linux / C++ / Gtk? [closed]
The boost::asio::io_service will be very useful for managing the udp sockets, but it doesn't actually give you any advantage when it comes to the event handling..
ether_ntoa pointers cast problem

Old Answer

I'm REALLY not sure what you're trying to do...

what you're saying doesn't make much sense.

I'll do my best to guess what you're trying to do, but I would suggest clarifying the question.. Question:.
Do I really need to use the main thread to send the data over component A socket or can I do it from the new-thread? (I think the answer is no, but I'm not sure about race conditions on sockets).
Answer:
You don't have to use the main thread to send data over the given component's socket.

Now depending on the socket library you're using there might be different restrictions: you may only be able to send data on the same thread that the socket was created, or you might be able to send data from any thread...

it really depends on the implementation of your socket.. Question:.
how to I wait for both events?.
Answer: . You can't do two things at the same time in the same thread...

with that said you have two options:.
  1. Constantly poll to see if either event has occurred (on the same thread).
  2. Have two threads that are blocking until a desired event occurs (usually when you read from a socket it blocks if there is no data).

Given the description of your problem it's unclear what you would achieve by using boost::condition_variable and/or boost::asio::io_service.

Perhaps you should give us a very simple example of code that we can follow.. Question:.
Is there any other alternative solution for this problem that I'm not aware of?.
Answer:
There are always alternative solutions out there, but it's really difficult to tell what the alternatives might be given the current description of the "problem." I think that you should edit the problem again and focus on providing very concrete examples, perhaps some pseudo code, etc..


2:


Switch to Windows and use WaitForMultipleObjects, or get this function implemented in Linux.

It's quite handy, and then you can do two things on the same thread..



74 out of 100 based on 39 user ratings 664 reviews