views:

129

answers:

1

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.
+1  A: 

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). Or is it?

Not that I'm aware of, and not without polling... you'll need a thread to wait for each asynchronous event. You can use a blocking stack or like you said use boost::condition_variable which blocks until there is something on the stack. 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.

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.

Lirik
thanks for your answer. based on the information you gave me I'll rethink my approach.
João Portela
edited my question. is it clear now?
João Portela
@João OK, I updated my answer... you're on the right track. Just split your sockets and event waiting into separate threads and you should be fine.
Lirik