说明,一个简短的小demo,记录下流程。
一 代码
源文件- C11线程池
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>
class ThreadPool {
public:
ThreadPool(size_t num = 4);
template<class F, class... Args>
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>;
~ThreadPool();
private:
// need to keep track of threads so we can join them
std::vector< std::thread > workers;
// the task queue
std::queue< std::function<void()> > tasks;
// synchronization
std::mutex queue_mutex;
std::condition_variable condition;
bool stop;
};
// the constructor just launches some amount of workers
inline ThreadPool::ThreadPool(size_t num )
: stop(false)
{
for(size_t i = 0;i< num;++i)
workers.emplace_back(
[this]
{
for(;;)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock,
[this]{ return this->stop || !this->tasks.empty(); });
if(this->stop && this->tasks.empty())
return;
task = std::move(this->tasks.front());
this->tasks.pop();
}
task();
}
}
);
}
// add new work item to the pool
template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>
{
using return_type = typename std::result_of<F(Args...)>::type;
auto task = std::make_shared< std::packaged_task<return_type()> >(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex);
// don't allow enqueueing after stopping the pool
if(stop)
throw std::runtime_error("enqueue on stopped ThreadPool");
tasks.emplace([task](){ (*task)(); });
}
condition.notify_one();
return res;
}
// the destructor joins all threads
inline ThreadPool::~ThreadPool()
{
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
condition.notify_all();
for(std::thread &worker: workers)
worker.join();
}
#endif
10年前的东西了,我还是写不出来。。。。。。
测试代码-头文件
#define DllExport __declspec( dllexport )//宏定义
DllExport int show();
DllExport int test();
源文件
#include "test.h"
#include <iostream>
#include "ThreadPool.h"
int show()
{
std::cout<<"hello world"<<std::endl;
return 1;
}
static ThreadPool pool;
int test()
{
return pool.enqueue(show).get();
}
CMakeLists
CMAKE_MINIMUM_REQUIRED(VERSION 3.1.1)
#解决方案名称
PROJECT(ThreadPoolDll)
#添加头文件路径
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
#添加源文件
AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} SRC_LIST)
#生成动态库
ADD_LIBRARY(DLL SHARED ${SRC_LIST})
#设置动态库存储路径
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
路径
二 步骤
进入到build文件夹,然后进入cmd中,
cmake …
然后打开叫DLL的解决方案,编就好了,记得编Release的。
用dumpbin检查一下
dumpbin -exports DLL.dll
检查导出符号,如果提示dumpbin 不是内部,外部符号啥的。
找到这两个文件
拖到命令行中,回车等下就好了。
三 测试
写个控制台就好了
#include <iostream>
#include "test.h"
int main()
{
std::cout << test() << std::endl;
std::cout << "Hello World!\n";
return 0;
}
1 项目-属性-C/C++ -常规-附加包含目录包含库的头文件
2 项目-属性-连接器-常规-附加库目录lib文件的位置
3 项目-属性-连接器-输入-附加依赖性,添加库的名字,这里是DLL.lib
运行-通过
四 其他
上述编译环境为VS2019,如果VS2015跑不起来,试试把测试文件中的全局静态变量
static ThreadPool pool; 换成局部的试一下,反正我的2015跑不起来。最后用别的方法搞了