C++ Thead多线程 跨平台线程池---C++11多线程快速学习

跨平台线程池实现

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

#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 个任务会被分配到不同的线程中执行。在任务执行完成后,程序会退出。

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
若依(Jui)是基于Bootstrap框架的前端UI库,而Bootstrap-table是一个用于显示数据表格的插件。如果想要在Bootstrap-table中实现多张图片的预览,你可以使用插件如Bootstrap-fileinput或图片懒加载(lazy loading)技术结合。 以下是基本步骤: 1. 首先,引入Bootstrap-table和所需的图片预览插件库,比如Bootstrap-fileinput: ```html <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/bootstrap-fileinput/5.3.1/css/fileinput.min.css"> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.1.3/js/bootstrap.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap-fileinput@5.3.1/dist/js/fileinput.min.js"></script> ``` 2. 在HTML结构中,为每个图片字段添加`data-file-input`属性来启用预览功能: ```html <table id="myTable" data-pagination="true" data-height="400"> <thead> <!-- ... --> </thead> <tbody> <tr> <td data-field="image" data-events="fileupload" data-url="api/upload-image">上传图片</td> <!-- 使用插件提供的控件,如:<input type="hidden" name="file" value=""> --> </tr> <!-- ... --> </tbody> </table> ``` 3. 使用Bootstrap-fileinput的JavaScript初始化表格和图片预览: ```javascript $('#myTable').bootstrapTable({ // ...其他配置 events: { 'click .fileinput': function(e, val) { $(this).fileinput('upload'); }, 'fileupload.bs.fileinput': function(e, file, previewId, index) { // 图片上传成功后,替换隐藏域的值并更新显示 $('#myTable').find('[data-field=image]').val(file.url); } } }); ``` 4. 为了实现图片预览,你可能还需要编写一个服务器端处理程序(例如API),返回文件的缩略图或者链接,然后在前端展示图片。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weijia3624

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值