lsalzman / enet

ENet reliable UDP networking library
MIT License
2.66k stars 667 forks source link

Trying to build a multi-threaded mwe #253

Open cuppajoeman opened 2 months ago

cuppajoeman commented 2 months ago

Hi there,

I've been trying to build a client-server architecture with enet, I've been running into some problems. I'm aware that enet doesn't have multi-threaded support, and I want to build a small example that allows for usage of enet from a multi-threaded point of view, so far I have this code:

https://github.com/cuppajoeman/mt_cs_enet

This is an example of multi-threaded usage of enet, WITHOUT any locking mechanisms. For example, when I run the server and two clients usually one of them segfaults (not immediately), with:

enet_list_remove (position=0x7ffff0001210) at /home/ccn/temp/enet_multithreaded/server/external_libraries/enet/list.c:37
37         position -> previous -> next = position -> next;

I don't really know what causes this problem, but I'm assuming it's the multi-threaded access of enet.


Would someone be willing to take a look at the code I linked to and propose a way to add mutex's to make it thread safe? I'm not quite sure how to approach it.

The files of interest are going to be:

The structure of the client is that there is a send and receive thread, the sending thread simply creates a struct, puts it into a packet and then sends it with enet_host_flush(...) and the receive thread calls enet_host_service(...) over and over.

The structure of the server is pretty much the same, along with a process thread which sits between the receive and send, when InputSnapshot's come in from the client, they get put on a thread-safe queue, which is then intermittently drained from the process thread, which increments a global num_inputs_snapshots_processed counter, the send thread simply sends this number back to the client.

The general structure tries to mimic that of a multiplayer game, but in the smallest possible way.