Questions tagged [threadpool]

A thread pool is a method to reuse existing threads, rather than always creating new ones. They allow for pooling resources in given limits and automatically assigning tasks to open workers. Use this tag when you have questions about implementing a thread pool, or using an existing thread pool implementation.

In multi-threaded applications, handling the various threads is a complicated task. Thread pool functionality is targeted to make it easier to use a large number of threads efficiently.

Because thread creation is relatively expensive, reusing threads in a pool can increase a programs performance. It can also help make better use of the available hardware threads by limiting the total number of threads that are created.

The main goal when using a thread pool is to be able to dynamically schedule work to a given set of workers. However, the original thread does not need to keep track of the thread creation itself, but forwards the thread management to the thread pool implementation. The thread pool will create new workers if necessary and destroy unused ones.

References

Related tags

4676 questions
51
votes
3 answers

Why does Windows 10 start extra threads in my program?

With Visual Studio 2015, in a new, empty C++ project, build the following for Console application: int main() { return 0; } Set a break point on the return and launch the program in the debugger. On Windows 7, as of the break point, this…
Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
50
votes
3 answers

multiprocessing.Pool - PicklingError: Can't pickle : attribute lookup thread.lock failed

multiprocessing.Pool is driving me crazy... I want to upgrade many packages, and for every one of them I have to check whether there is a greater version or not. This is done by the check_one function. The main code is in the Updater.update method:…
rubik
  • 8,814
  • 9
  • 58
  • 88
50
votes
7 answers

Thread.Start() versus ThreadPool.QueueUserWorkItem()

The Microsoft .NET Base Class Library provides several ways to create a thread and start it. Basically the invocation is very similar to every other one providing the same kind of service: create an object representing an execution flow (or more),…
Andry
  • 16,172
  • 27
  • 138
  • 246
49
votes
5 answers

WaitAll for multiple handles on a STA thread is not supported

Why do I get this error message? "WaitAll for multiple handles on a STA thread is not supported." Should I use [MTAThreadAttribute] attribut? Update: Dosn't work with WPF applications! Note: It error is at line WaitHandle.WaitAll(doneEvents); …
Amir Rezaei
  • 4,948
  • 6
  • 33
  • 49
47
votes
7 answers

Why are OS threads considered expensive?

There are many solutions geared toward implementing "user-space" threads. Be it golang.org goroutines, python's green threads, C#'s async, erlang's processes etc. The idea is to allow concurrent programming even with a single or limited number of…
Chi-Lan
  • 3,575
  • 3
  • 22
  • 24
46
votes
4 answers

How to use a goroutine pool

I want to use Go for downloading stock price spreadsheets from Yahoo finance. I'll be making an http request for every stock in its own goroutine. I have a list of around 2500 symbols, but instead of making 2500 requests in parallel, I'd prefer…
tldr
  • 11,924
  • 15
  • 75
  • 120
45
votes
6 answers

How to catch exceptions from a ThreadPool.QueueUserWorkItem?

I have the following code that throws an exception: ThreadPool.QueueUserWorkItem(state => action()); When the action throws an exception, my program crashes. What is the best practice for handling this situation? Related: Exceptions on .Net…
Michael Hedgpeth
  • 7,732
  • 10
  • 47
  • 66
45
votes
6 answers

Exception handling in ThreadPools

I have a ScheduledThreadPoolExecutor that seems to be eating Exceptions. I want my executor service to notify me if a submitted Runnable throws an exception. For example, I'd like the code below to at the very least print the…
Ivan
  • 1,256
  • 1
  • 9
  • 16
43
votes
7 answers

C++ Thread Pool

What is a good open source implementation of a thread pool for C++ to use in production code (something like boost)? Please provide either your own example code or a link to example code usage.
Amir Rachum
  • 76,817
  • 74
  • 166
  • 248
42
votes
4 answers

how are concurrent requests handled in PHP (using - threads, thread pool or child processes)

I understand that PHP supports handling multiple concurrent connections and depending on server it can be configured as mentioned in this answer How does server manages multiple connections does it forks a child process for each request or does it…
prasun
  • 7,073
  • 9
  • 41
  • 59
40
votes
6 answers

Propagating ThreadLocal to a new Thread fetched from a ExecutorService

I'm running a process in a separate thread with a timeout, using an ExecutorService and a Future (example code here) (the thread "spawning" takes place in a AOP Aspect). Now, the main thread is a Resteasy request. Resteasy uses one ore more…
Luciano Fiandesio
  • 10,037
  • 10
  • 48
  • 56
40
votes
1 answer

What are the differences between event-driven and thread-based server system?

Node.js is an event driven I/O and It's a single threaded server that acts upon callbacks and never blocks on the main thread. But how does it manage to non-blocking I/O? if it does easy to manage, why don't thread-based system manage it? Does not…
erginduran
  • 1,678
  • 5
  • 28
  • 51
38
votes
4 answers

Existing threadpool C implementation

What open-source implementation(s) in C for a pthreads thread pool would you recommend ? Additional points if this implementation is : Light-weight: glib, APR, NSPR and others come with a big buy-in, I'd rather have just 2 files (header and…
Mathias Brossard
  • 3,668
  • 2
  • 26
  • 30
38
votes
1 answer

Calling condition.wait() inside thread causes retrieval of any future to block on main thread

I have tasks that are executed inside a threadpool that share a reentrant read write lock. these tasks return futures if there execution finishes. The reentrant read write lock will wait on a condition when the lock experiences contention. The…
user2229108
38
votes
7 answers

What is the use of a Thread pool in Java?

What is the use of a Thread pool? Is there a good real world example?
JavaUser
  • 25,542
  • 46
  • 113
  • 139