Original Post
Hello,
I have a newbie question regarding multithreaded networking. I have an application that uses eNet to pass and receive messages between a client and server. In this simple implementation I only need the server to pass data to the client. I have certain messages that come in different packets but the client needs to "handle" them at the same time for synchronicity issues between gameplay objects (This is a constraint by the way and cannot be changed for this project). My desired goal is to have the following
-the client spawns a new thread which reads network packets off the UDP stream (this way network traffic is always being serviced regardless of framerate, so you can't get false timeouts from another piece of code)
-the packets are stored into a dynamic array (STL vector)
-the application (main process/thread) processes all the packets in the array when it is time for an update
My questions are the following:
What is the best strategy to make the vector thread-safe (since I can't have the network thread adding packets, while the main thread tries to read simultaneously)?
Or maybe the better question is what is the best strategy to let the main thread know when the data is ready for processing/consumption and vice-versa (main thread tells network thread to collect more packets from the stream again)?
The only problem with the second idea is that I don't want the network thread to hang/wait on collecting packets while the main thread does the processing. Should I use two vectors (one local in the network thread that is always filling and one shared between the two and then use an event?) Thanks in advance for helping a beginner - R
I have a newbie question regarding multithreaded networking. I have an application that uses eNet to pass and receive messages between a client and server. In this simple implementation I only need the server to pass data to the client. I have certain messages that come in different packets but the client needs to "handle" them at the same time for synchronicity issues between gameplay objects (This is a constraint by the way and cannot be changed for this project). My desired goal is to have the following
-the client spawns a new thread which reads network packets off the UDP stream (this way network traffic is always being serviced regardless of framerate, so you can't get false timeouts from another piece of code)
-the packets are stored into a dynamic array (STL vector)
-the application (main process/thread) processes all the packets in the array when it is time for an update
My questions are the following:
What is the best strategy to make the vector thread-safe (since I can't have the network thread adding packets, while the main thread tries to read simultaneously)?
Or maybe the better question is what is the best strategy to let the main thread know when the data is ready for processing/consumption and vice-versa (main thread tells network thread to collect more packets from the stream again)?
The only problem with the second idea is that I don't want the network thread to hang/wait on collecting packets while the main thread does the processing. Should I use two vectors (one local in the network thread that is always filling and one shared between the two and then use an event?) Thanks in advance for helping a beginner - R