8. C++11 跨平台线程池

跨平台线程池实现

它使用 C++11 标准库中的 std::thread、std::mutex、std::condition_variable、std::function 和 std::queue 等组件实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <queue>

class ThreadPool {
public:
ThreadPool(int numThreads) : stop(false) {
for (int i = 0; i < numThreads; ++i) {
threads.emplace_back([this] {
while (true) {
std::unique_lock<std::mutex> lock(mutex);
condition.wait(lock, [this] { return stop || !tasks.empty(); });
if (stop && tasks.empty()) {
return;
}
std::function<void()> task(std::move(tasks.front()));
tasks.pop();
lock.unlock();
task();
}
});
}
}

~ThreadPool() {
{
std::unique_lock<std::mutex> lock(mutex);
stop = true;
}
condition.notify_all();
for (std::thread& thread : threads) {
thread.join();
}
}

template<typename F, typename... Args>
void enqueue(F&& f, Args&&... args) {
std::function<void()> task(std::bind(std::forward<F>(f), std::forward<Args>(args)...));
{
std::unique_lock<std::mutex> lock(mutex);
tasks.emplace(std::move(task));
}
condition.notify_one();
}

private:
std::vector<std::thread> threads;
std::queue<std::function<void()>> tasks;
std::mutex mutex;
std::condition_variable condition;
bool stop;
};

int main() {
ThreadPool pool(4);
for (int i = 0; i < 8; ++i) {
pool.enqueue([i] {
std::cout << "Task " << i << " is running in thread " << std::this_thread::get_id() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Task " << i << " is done" << std::endl;
});
}
return 0;
}

在这个示例中,我们同样定义了一个 ThreadPool 类,并且在构造函数中创建了指定数目的线程。在每个线程中,我们不断地从任务队列中获取任务并执行,直到线程池被停止。在 enqueue() 函数中,我们将任务封装成一个 std::function 对象,并将它添加到任务队列中。在 ThreadPool 的析构函数中,我们等待所有线程执行完成后再停止所有线程。

在主函数中,我们创建了一个 ThreadPool 对象,并向任务队列中添加了 8 个任务。每个任务会输出一些信息,并且在执行完后等待 1 秒钟。由于线程池中有 4 个线程,因此这 8 个任务会被分配到不同的线程中执行。在任务执行完成后,程序会退出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <thread>
#include <mutex>
#include <string>
#include <condition_variable>
#include <queue>
#include <vector>
#include <functional>
class ThreadPool {
public:
ThreadPool(int numThreads) :stop(false) {
for (int i = 0; i < numThreads; i++) {
threads.emplace_back([this] {
while (1) {
std::unique_lock<std::mutex> lock(mtx);
condition.wait(lock, [this] {
return !tasks.empty() || stop;
});
if (stop && tasks.empty()) {
return;
}
std::function<void()> task(std::move(tasks.front()));
tasks.pop();
lock.unlock();
task();
}
});
}
}

~ThreadPool() {
{
std::unique_lock<std::mutex> lock(mtx);
stop = true;
}


condition.notify_all();
for (auto& t : threads) {
t.join();
}
}
template<class F, class... Args>//函数模板里右值引用是万能引用
void enqueue(F&& f, Args&&... args) {
std::function<void()> task =
std::bind(std::forward<F>(f), std::forward<Args>(args)...);
{
std::unique_lock<std::mutex> lock(mtx);
tasks.emplace(std::move(task));
}
condition.notify_one();

}


private:
std::vector<std::thread> threads;
std::queue<std::function<void()>> tasks;

std::mutex mtx;
std::condition_variable condition;

bool stop;

};

int main()
{
ThreadPool pool(4);

for (int i = 0; i < 10; i++) {
pool.enqueue([i] {
std::cout << "task : " << i << " is running" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "task : " << i << " is done" << std::endl;
});
}
return 0;
}

8. C++11 跨平台线程池
http://binbo-zappy.github.io/2024/11/27/cpp-多线程/8-Cpp11 跨平台线程池/
作者
Binbo
发布于
2024年11月27日
许可协议