0

I am trying to write a server program which supports one client till now and over the few days i was trying to develop it, I concluded i needed threads. The reason for such a decision was since I take input from a wifi socket and later process it and finally write to a file, the processing time is slow and hence i needed a input thread -> circular buffer -> output thread pattern with producer consumer model which is quite common in network programming.

Now, The situation becomes complicated, as I need to manage client disconnection and re connection. I thought of using pthread_exit() and cleaning up all the semaphores and then re initializing them each time the single client re connects.

My question is that is this a efficient approach i.e. everytime killing the threads and semaphores and re creating them. Are there any better solutions.

Thanks.

Haswell
  • 1,573
  • 1
  • 18
  • 45

2 Answers2

0

My question is that is this a efficient approach i.e. everytime killing the threads and semaphores and re creating them. Are there any better solutions.

  1. Learn how to use non-blocking sockets and an event loop. Or use a library that provides TCP sessions for you using non-blocking sockets under the hood. Such as boost::asio.
  2. Learn how to use multi-threading without polluting your code with any synchronization primitives by using message passing to communicate between threads, not shared state. The event loop library you use for non-blocking I/O should also provide means for cross-thread message passing.
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
-1

Some comments and suggestions.

1-In TCP detecting that the other side has silently disconnected it very difficult if not impossible. A client could disconnect sending a RST TCP message to the server or sending a FIN message, this is the good case. Sometimes the client can disconnect without notice (crash, cable disconnection, etc).

  • One suggestion here is that you consider the way client and server will communicate. For example, you can use function “select” to set a timeout for receiving a message from client and detect a silent client.

  • Additionally, depending on the programming language and operating system you may need to handle broken pipe (SIGPIPE) signal (in Linux, with C/C++), for a server trying to send a message through a connection closed by the client.

2-Regarding semaphores, you shouldn’t need to clean semaphores in any especial way when a client disconnect. By applying common good practices of locking and unlocking mutexes should be enough. Also with resources like file descriptors, you need to release them before ending the thread either by returning from the thread start function or with pthread_exit. Maybe I didn’t understand this part of the question.

3-Regarding threads: if you work with multiple threads to optimum is to have a pool of pre-created consumer/worker threads that will check the circular buffer to consume the next available connection. Creating and destroying threads is costly for the operating system.

  • Threads are resource consuming and you may exhaust operating system resources if you need to create 1,000 threads for example.

  • Another alternative, is to have only one consumer thread that manages all connections (sockets) asynchronously: a) Each connection has its own state. b) The main thread goes through all connections and use function “select” to detect when connection reads or a writes are ready. 3)Use of non-blocking sockets but this is not essential because from select you know which sockets are ready and will not block.

  • You can use functions select, poll, epoll.

One link about select and non-blocking sockets: Using select() for non-blocking sockets Other link with an example: http://linux.die.net/man/2/select

Community
  • 1
  • 1
rodolk
  • 5,606
  • 3
  • 28
  • 34
  • As edited I am using simple flags to signal threads to stop and ultimately join. It works well for two connections, the third connection faces issues. Should I put some delay while closing the previous socket and new socket. – Haswell Sep 02 '14 at 07:40
  • I am closing all the descriptors before creating any new socket fd's for new connection. – Haswell Sep 02 '14 at 08:53
  • Aren't you using standard socket functions like bind, listen, accept, etc? The code you pasted doesn't make much sense to me. – rodolk Sep 03 '14 at 02:45