Hisham Hijjawi
Hisham Hijjawi

Reputation: 2425

Why doesn't C++ have a std::thread_pool in the standard library?

It just seems strange to me that despite having a very large set of constructs for multithreading, the standard lacks a thread pool class. What reasons might dissuade the committee from adding this to the standard?

Upvotes: 10

Views: 2454

Answers (1)

Gabe
Gabe

Reputation: 115

C++, like C, is meant to give as much control to the programmer as possible. Almost everything in C++ is a wrapper that is very bare-bones. This give the programmer freedom to implement whatever feature they want however they want.

The concept of "what is work" is a bit abstract and dependent on the use case, so C++ gives you the workers (threads), and lets you define a strategy for how you want that work to be distributed amongst the workers.

For example, in Python, you can map work to threads. Using this means that whenever work is available, a thread will take the work. But what if you want a thread to only do work if there is work to do AND after certain conditions are met. You can design your thread_pool class to meet all these specifications. In Python, you'd have to handle these checks separately outside of the thread pooling library.

While there is no OFFICIAL answer, this is the answer that I would say makes more sense. C++ is about control given a minimal amount of tools (however an EXTENDED set compared to C). The committee is most likely not adding a thread_pool class because the hardest thing to do in Computer Science is getting people to agree. Thread pooling is not necessarily extremely hard to implement, and defining a definition of worker is arguably harder.

Upvotes: 2

Related Questions