Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is better: Select vs Threads?

In linux.

I want to build an autoclicker that will have an enable/disable function when a key is pressed. Obviously there should be 2 things running in parallel (the clicker itself, and the enable/disable function)

What are the cons and pros of each implementation: Using a thread which will handle the autoclicking function and another main thread (for the enable/disable etc...) Or using the syscall select and wait for input/keyboard?

like image 342
Jah Avatar asked May 06 '12 21:05

Jah


People also ask

Which is better multithreading or single threading?

Advantages of Multithreaded Processes All the threads of a process share its resources such as memory, data, files etc. A single application can have different threads within the same address space using resource sharing. It is more economical to use threads as they share the process resources.

What does single threaded mean?

the execution of an entire task from beginning to end without interruption.

Why multi thread is faster?

In many cases, multithreading gives excellent results for I/O bound application, because you can do multiple things in parallel, rather than blocking your entire app waiting for single I/O operation. This is also most common case when using more threads than cpu cores is beneficial.


1 Answers

Using select is better for performance, especially when you could have potentially hundreds of simultaneous operations. However it can be difficult to write the code correctly and the style of coding is very different from traditional single threaded programming. For example, you need to avoid calling any blocking methods as it could block your entire application.

Most people find using threads simpler because the majority of the code resembles ordinary single threaded code. The only difficult part is in the few places where you need interthread communication, via mutexes or other synchronization mechanisms.

In your specific case it seems that you will only need a small number of threads, so I'd go for the simpler programming model using threads.

like image 128
Mark Byers Avatar answered Sep 29 '22 21:09

Mark Byers