You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I’ve implemented a Modbus TCP server that accepts multiple concurrent client connections. For each new connection, the server spawns a new thread to handle the specific client’s requests.
Initially, I assumed that the listening socket was handled by a single Modbus context, and each client would be managed as an independent connection within this context.
Here’s what I did:
I passed the same Modbus context and unique clientSocket to each thread.
I used modbus_set_socket() within each thread, expecting that it would handle individual client connections independently.
Issue: It appears that modbus_set_socket() modifies a single property in the shared Modbus context rather than managing it on a per-thread basis. Consequently, when multiple threads attempt to set the socket within the same context, the behavior becomes inconsistent and connections get mixed up.
Questions:
Would it be viable to create a shallow copy of the Modbus context for each connection and then use modbus_set_socket() on each copy?
Is there a recommended approach to implementing multi-threaded handling of client connections, or is multi-threading considered problematic in libmodbus?
Any advice on best practices for this situation would be highly appreciated.
The text was updated successfully, but these errors were encountered:
Hello,
I’ve implemented a Modbus TCP server that accepts multiple concurrent client connections. For each new connection, the server spawns a new thread to handle the specific client’s requests.
Initially, I assumed that the listening socket was handled by a single Modbus context, and each client would be managed as an independent connection within this context.
Here’s what I did:
Issue: It appears that modbus_set_socket() modifies a single property in the shared Modbus context rather than managing it on a per-thread basis. Consequently, when multiple threads attempt to set the socket within the same context, the behavior becomes inconsistent and connections get mixed up.
Questions:
Any advice on best practices for this situation would be highly appreciated.
The text was updated successfully, but these errors were encountered: