I wrote some regular fstream file code for handling the file that stores their information and I was about to implement it into the server, then I realized there might be a problem having it open multiple times concurrently |
There is no problem opening multiple files at the same time. The OS is designed to do that.
I did a bit of looking around, and found a site that was using windows critical sections, would simply putting this around the resource i want to restrict do the job? |
You only need to synchronise data structures that are accessed from different threads of execution. If a data structure isn't accessed from multiple threads, there's nothing to synchronise.
The problem is, even when just RECEIVING data from both of the clients, using critical sections as I understand they are supposed to be used, the data is extremely jumbled in the text file. When I remove the critical sections it is (seemingly) almost just as garbled. |
It's difficult to help without knowing how the server works. We can't speculate on how you're handling this received data, so we can't comment on synchronisation.
Could some explain to me a bit more on how critical sections actually work, and what method I should be using for sending character data like this? Is it necessary to store it to a file or can I share variables between threads somehow? |
A Windows Critical Section is Mutux mechanism that works within process. It guarantees that only on thread will execute a section of code at a time. It's more efficient than using a Mutex because is doesn't force a context switch (which is particularly expensive on Windows). That's probably all you need to know at this point.
As for your server maintaining a model of the clients views, each client starts doing stuff from a know state; the server will modle this state in some data strucutres. If a client changes state, it needs to notify the server, so it sends an update to the server.
The server knows what state the client started in, and listens for updates from its clients. These updates are then applied to the servers data structures (model). These updates need to be synchronised if data is received on different threads, but the whole thing can be done in a single thread. Threading isn't the issue, it's your algorithm for sending/applying the updates and your protocol that the clients/server use to communicate that are the big design features.
Is asynchronous I/O the absolute best way to go? |
No. It's an optimisation that removes some wait states at the cost of complexity. But if you don't have a working system, you don't have anything to optimise (yet).