How should a thread pool be implemented in C?
Yes, you can create a thread-safe queue between the threads. Then the threads in the pool will sit in a loop retrieving an item from the queue, executing whatever it needs, then going back and getting another.
That's generally a bit easier/simpler in C++ because it's a little easier to agree on some of the interface (e.g., overload operator()
to execute the code for a task), but at a fundamental level you can do all the same things in C (e.g., each task
struct you put in the queue will contain a pointer to a function to carry out the work for that task).
In your case, since you are using C++, it's probably easier to use an overload of operator()
to do the work though. The rest of the task
struct (or whatever you choose to call it) will contain any data needed, etc.
Is there a function to join threads from thread pool in glib?
Call g_thread_pool_free (th_pool, TRUE, TRUE)
. It will wait for all pending tasks to finish running, and then free the memory used by the GThreadPool
.
C++ : Throw Away threads vs Thread Pool
You could use std::launch as some people have suggested. Or you could do a google for "c++ thread pool library" and probably find something waiting for you.
But the reality is simple: writing a thread pool is close to trivial and is a good exercise. So you could write your own and learn something. As has been suggested, you can dispatch via some sort of message queue.
A work queue would be any sort of mutex & cond_var - managed FIFO, and then you can have multiple readers and multiple writers. The entries in the queue can be any sort of runnable or bound function (such as a lambda).
Fun to write and you can toss into your person library for years to come.
Debugging the use of std::string in a thread pool C++
Destructors are called in the order opposite to variable declaration order. i.e. v
will be destructed earlier than pool
, therefore at the moment when some threads from pool will call to printString()
, the argument string will not be a valid object, because v
and its content are already destroyed. To resolve this, I'd recommend to declare v
before pool
.
C++ Thread Pool
I think it is still not accepted into Boost, but a good staring point:
threadpool. Some example of usage, from the web site:
#include "threadpool.hpp"
using namespace boost::threadpool;
// Some example tasks
void first_task()
{
...
}
void second_task()
{
...
}
void third_task()
{
...
}
void execute_with_threadpool()
{
// Create a thread pool.
pool tp(2);
// Add some tasks to the pool.
tp.schedule(&first_task);
tp.schedule(&second_task);
tp.schedule(&third_task);
// Leave this function and wait until all tasks are finished.
}
The argument "2" to the pool indicates the number of threads. In this case, the destruction of tp
waits for all threads to finish.
Related Topics
How to Use a Timer in C++ to Force Input Within a Given Time
C++ Read/Write Class From/To Binary File
How to Serialize Boost::Dynamic_Bitset
Spirit-Qi: How to Write a Nonterminal Parser
Is Msiopenproduct the Correct Way to Read Properties from an Installed Product
Difference Between Initialization of Static Variables in C and C++
How to Make Cout Behave as in Binary Mode
What Is the Vtable Layout and Vtable Pointer Location in C++ Objects in Gcc 3.X and 4.X
Why Doesn't C++ Move Construct Rvalue References by Default
Sizeof(Struct) Returns Unexpected Value
Sizeof in C++ Showing String Size One Less
What Operators Do I Have to Overload to See All Operations When Passing an Object to a Function
C++ Error, Undefined Reference Class
How Does C++ Preprocessors Work
Does Accessing the First Field of a Struct via a C Cast Violate Strict Aliasing