Threading the right way for my business?

I am currently developing an application for several clients / servers. I use plain old old sockets because WCF or similar technology is not what I need. Let me explain: this is not a classic case where a customer simply calls a service; all clients can "interact" with each other by sending a packet to the server, which then performs an action and can resend the response message to one or more clients. While this is possible with WCF, the application will be quite complex with hundreds of different messages.

For each connected client, of course, I use asynchronous methods to send and receive bytes. I have messages fully working, everything is in order. Except for every line of code that I write, my head just burns due to multi-threaded problems. Since about 200 clients can be connected at the same time, I decided to use multithreading: every message received in the juice is immediately processed in the thread pool thread that was received, and not on one consumer thread.

Since each client can interact with other clients and indirectly with shared objects on the server, I have to protect almost every object that is changed. I first went with ReaderWriterLockSlimfor each resource that should be protected, but quickly noticed that there was more writing in the general application than readings in the server application, and switched to the known Monitorone to simplify the code.

So far so good. Each resource is protected, I have helper classes that I must use to get the lock and its protected resource, so I cannot use the object without the lock. In addition, each client has its own lock, which is entered as soon as the packet is received from its socket. This is done so that other clients do not make changes to the state of this client as long as it has processed messages, which often happens.

Now I do not need to simply protect resources from simultaneous access. I have to keep every client in sync with the server for some of the collections that I have. One tricky part that I'm facing right now is this:

  • I have a collection of clients. Each client has its own unique identifier.
  • , , .
  • , , .
  • , , , . , # 1, , " №2 - ", , : 1 " 2 ?".

( X) (, newClient ):

lock (clients) {
  foreach (var client in clients) {
    lock (client) {
      client.Send("newClient with id X has connected");
    }
  }
  clients.Add(newClient);
  newClient.Send("the list of other clients");
}

, , , , - (, - Y - ):

lock (clients) {
  foreach (var client in clients) {
    lock (client) {
      client.Send("something");
    }
  }
}

: X , clients , Y... , !

, . , , .. , . , , . , , , / ... , , , , .

, : , , : ? .NET framework, #, concurrency . , . , , , . , ... , . ?

, StackOverflow, , . , .

+3
3

Erlang , . Erlang , , , .

http://en.wikipedia.org/wiki/Erlang_(programming_language)

, , ( Erlang) , . , , Erlang , .

#, Erlang - Retlang:

http://code.google.com/p/retlang/wiki/GettingStarted

, , , , , .

+2

, .. - , . . , . , - . . , , , , .

+4

.NET, C Linux.

, , : ( ) . , ( ), . , , , , -. , , . , , - , (, DOS) . , OS MMU ( ) .

, , PostgreSQL. , Todo ", " (. ). , , ( , ), ( ) PostgreSQL.

, . :  * , . , URL- , , , , URL- (, ).  * , , .

. , , , , .

+1
source

Source: https://habr.com/ru/post/1746613/


All Articles