开发者

Is it okay to create threads instead of forking process in server in socket connections?

I am making a simple progr开发者_如何学运维am which suppose to accept txt file data from clients. (not very big files..)

I was wondering, if it is okay to create the threads using pthread instead of creating another process using fork.

My plan is to create threads for every connection that I get to process the data and store it in a shared queue, which I can use mutex or semaphore to protect any race condition.


Yes, it is perfectly fine to use separate threads to process client connections. It will be faster compared to forking new processes. There is a drawback though in terms of isolation: because all clients are handled within the same process if this process goes down the server will not be able to serve other clients.


As long as you keep in mind the limitations of threads, it's fine. In particular, you're going to need appropriate locking, and you'll have relatively small stacks to work with.

You might also want to consider an event driven architecture using eg libevent; this avoids the locking and stack size issues, and reduces context switch overhead, but can result in more complex code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜