C++ 手搓线程池(改进)
本文最后更新于23 天前,其中的信息可能已经过时,如有错误请留言

具体代码如下:

#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <functional>
#include <queue>
#include <iostream>

class ThreadPool {
private:
    std::vector<std::thread> threads;
    std::mutex mtx;
    std::condition_variable cond;
    std::atomic<bool> stop;
    std::queue<std::function<void()>> tasks;
    ThreadPool(const ThreadPool&) = delete;
    ThreadPool& operator=(const ThreadPool&) = delete;
public:
    ThreadPool(int threadNum):stop(false) {
        for (int i = 0; i < threadNum; ++i) {
            threads.emplace_back([this](){
                std::cout << std::this_thread::get_id() << "th thread start running..." << std::endl;
                std::function<void()> task;
                while (true) {
                    {
                        std::unique_lock<std::mutex> lock(mtx);
                        while(!stop.load() && tasks.empty()) {
                            cond.wait(lock);
                        }
                        if (stop.load() && tasks.empty()) {
                            return;
                        }
                        task = std::move(tasks.front());
                        tasks.pop();
                    }
                    try {
                        task();
                    } catch(...) {
                        std::cout << "task error" << std::endl;
                    }
                }
            });
        }
    }
    ~ThreadPool() {
        Stop();
    }
    void Stop() {
        if (stop.exchange(true)) {
            return;
        }
        cond.notify_all();
        for (std::thread& th : threads) {
            if (th.joinable()) {
                th.join();
            }
        }
    }
    void AddTask(std::function<void()>&& task) {
        if (stop.load()) {
            return;
        }
        {
            std::lock_guard<std::mutex> lock(mtx);
            tasks.emplace(std::move(task));
        }
        cond.notify_one();
    }
};

int main() {
    ThreadPool pool(3);
    pool.AddTask([](){
        std::cout << std::this_thread::get_id() << "th thread executing task1..." << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(2));
        std::cout << "task1 end..." << std::endl;
    });
    pool.AddTask([](){
        std::cout << std::this_thread::get_id() << "th thread executing task2..." << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(2));
        std::cout << "task2 end..." << std::endl;
    });
    pool.AddTask([](){
        std::cout << std::this_thread::get_id() << "th thread executing task3..." << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(2));
        std::cout << "task3 end..." << std::endl;
    });
    return 0;
}

编译脚本如下:

# Makefile

# 编译器
CXX := g++

# 编译选项
CXXFLAGS := -std=c++11 -pthread

# 目标文件名
TARGET := main

# 源文件(可以在这里添加多个 .cpp 文件)
SRC := code.cpp

# 自动生成对应的 .o 文件列表
# OBJ := $(SRC:.cpp=.o)

# 最终目标
$(TARGET): $(SRC)
	$(CXX) $(CXXFLAGS) -o $(TARGET) $(SRC)

# 通用规则:把每个 .cpp 编译成 .o
# %.o: %.cpp
#     $(CXX) $(CXXFLAGS) -c $< -o $@

感谢阅读!如有疑问请留言
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇