Your case sounds like a typical Unix service daemon. A popular solution to your problem is not using threads, but plugs.
The idea is that your program is listening on a socket and waiting for connections. As soon as the connection comes, it forks. Then the child process continues to process the connection. The fatherβs process itself simply continues in a loop and waits for incoming connections.
Benefits for streaming:
- Very simple program design.
- No problem with concurrency
- Installed Method for Unix / Linux Systems
Disadvantages:
- Things get more complicated when multiple connections interact with each other (your use case is not like them)
- Decreased performance on Windows systems (not Unix systems!)
You can find many sample code on the Internet.
ypnos source share