Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for a Linux threadpool api with OS scheduler support

I'm looking for a thread pool abstraction in Linux that provides the same level of kernel scheduler support that the Win32 thread pool provides. Specifically, I'm interested in finding a thread pool that maintains a certain number of running threads. When a running pool thread blocks on I/O, I want the thread pool to be smart enough to start another thread running.

Anyone know of anything like this for linux?

like image 696
John Calcote Avatar asked Jun 04 '12 17:06

John Calcote


People also ask

When should you not use Threadpool?

Thread pools do not make sense when you need thread which perform entirely dissimilar and unrelated actions, which cannot be considered "jobs"; e.g., One thread for GUI event handling, another for backend processing. Thread pools also don't make sense when processing forms a pipeline.

Which of the following operating system supports many to many model?

4.7. Win32 also provides the fiber library, which supports the many-to-many model. Win32 thread components include: Thread ID. Registers.

What is thread explain the threads in Linux?

Threads are a popular modern programming abstraction. They provide multiple threads of execution within the same program in a shared memory address space. They can also share open files and other resources. Threads allow for concurrent programming and, on multiple processor systems, true parallelism.

Which of the following system calls is are used to create a thread?

The underlying system call to create threads is clone(2) (it is Linux specific).


1 Answers

You really can't do this without OS support. There's no good way to tell that a thread is blocked on I/O. You wind up having to atomically increment a counter before each operation that might block and decrement it after. Then you need a thread to monitor that counter and create an additional thread if it's above zero. (Remove threads if they're idle more than a second or so.)

Generally speaking, it's not worth the effort. This only works so well on Windows because it's the "Windows way" and Windows is built from the ground up for it. For Linux, you should be using epoll or boost::asio. Use something that does things the "Linux way" rather than trying to make the Windows way work on non-Windows operating systems.

You can write your own wrappers that use IOCP on Windows, epoll on Linux, and so on. But these already exist, so you need not bother.

like image 182
David Schwartz Avatar answered Oct 01 '22 12:10

David Schwartz