Friday, 15 August 2014

c - Using select() then read() vs a blocking read() loop -


I am working on a multi-threaded server code where I make 2 threads with the blocking TCP socket Accept () .

Thread # 1 check to see if the message queue has commands and writes it in the socket Thread # 2 is only a simple while the loop which calls read And then there is no data ready to read the blocks.

However my colleague told me that while I will do this as a loop with a read it will ruin the CPU chakras (this is a blocked read is, does the thread not sleep?), That I want to select () read the socket then the system call.

Am I wrong to handle the thread? Which approach is better?

You are right that does not consume CPU while blocking on thread I / O.

Select () syscall is a viable option, but in the case of its main use I / O multi-channel / with multiple channels. In that case, it reads (or writes) all channels for a thread, because it is often blocked waiting for data from one channel, while different data was ready and waiting. On the other hand, you do not want to read non-blocked in a loop, as will garbage CPU.

To choose whether to block any channel for this, click on Reading () to select or () (or An orange comparison with an apple is preventing the poll () , but if these are the options then it is easier to block at reading () . What I will do is compare apples-to-apples on the lines of many threads or hair processes, each one can create a separate channel and block it on reading () , versus a thread of a process with which Many channels are managed by the help () .


No comments:

Post a Comment