LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Pthread VS select (https://www.linuxquestions.org/questions/programming-9/pthread-vs-select-166708/)

kris273 04-06-2004 12:18 AM

Pthread VS select
 
I was curious about the issues and major differences between using Pthread and select to handle multiple clients that will be logged on for an undetermined amount of time.

One way I see is to make detached threads handle every client, each client has a thread devoted to listening to him, and it takes the data, processes it, and returns it on another thread.

the other way is using select, check for input, if so, process and go on.


is there one better or worse than the other? easier or harder? are there any major issues using 2 different threads to communicate with a client on the same socket addr? (is there still an issue if you used multex lock?)


Kris

Mara 04-06-2004 05:22 PM

Both methods are good. Both have advantages and disadvantages.

Threads
* new thread for every client means extra resources for a new client
* functions to handle clients are simplier but
* synchronization required

Select:
* one process, no extra resources for a new client (theory)
* more compilicated function to handle clients, many more cases to take care of
* no synchronization

I must say that multiclient code with select is usually much more compilicated than using threads. Of course, it also depends if you know how to work with threads or not...

There's also a possibility to use both: threads and select in one program. An exaple: WWW server that has new thread for every client (has threads waiting for clients, if there are not enought idle threads new ones are created, if there are too many - they may be killed). Select can be used as a click in the main server function (listening for a new connection). With a limited select time you get a possibility to look if there are enough threads (and create or delete a number of them). Then you can run select again.


All times are GMT -5. The time now is 02:31 AM.