我正在尝试使用线程池更新向量,其中每个工作者负责更新向量的某些部分。更具体地说,我将矢量分割成具有空交集的子集,其中集合的数量由我的计算机可用的cpus数量给出(4)。
每个线程工作者都会更新:
std::array<int, arraySize> vec
如果在类MultiThreadedUpdateOfVector
中将此数组声明为私有,则该功能不起作用:每次交易都会更新vec
的相应部分,但下一个线程不会选择更改。因此,vec就好像是每个线程的局部变量。
如果出现以下情况,此问题就会消失:
std::array<int, arraySize> vec
在MultiThreadedUpdateOfVector
之前声明。
你能解释一下这种不受欢迎的行为吗?
您能否建议std::array<int, arraySize> vec
仍然是MultiThreadedUpdateOfVector
成员的解决方案?
谢谢!
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <array>
#include "ThreadPool.h"
using namespace std;
const int block = 2;
const int arraySize = 8;
class MultiThreadedUpdateOfVector
{
public:
MultiThreadedUpdateOfVector()
{
}
bool setArray(const int& i, const int& j)
{
std::thread::id id = std::this_thread::get_id();
for (int kk = i; kk < j; ++kk)
{
vec[kk] = i * 10000 + j * 100 + kk;
}
return true;
}
void print()
{
for (unsigned kk = 0; kk < vec.size(); ++kk)
{
std::cout << kk << " " << vec[kk] << endl;
}
}
private:
std::array<int, arraySize> vec;
};
int main()
{
std::thread::id id = std::this_thread::get_id();
ThreadPool pool(4);
std::vector<std::future<bool> >results;
MultiThreadedUpdateOfVector h;
int begin = 0;
int end = block;
for (int i = 0; i < 4; ++i) {
results.push_back(pool.enqueue(&MultiThreadedUpdateOfVector::setArray, h, begin, end));
begin = end + 1;
end += block;
}
for (int i = 0; i < 4; ++i)
results[i].get();
h.print();
return 0;
}
#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);
template<class F, class... Args>
auto enqueue(F&& f, Args&&... args)
->std::future<typename std::result_of<F(Args...)>::type>;
~ThreadPool();
int getTasksSize()
{
std::unique_lock<std::mutex> lock(this->queue_mutex_m);
std::thread::id id1 = std::this_thread::get_id();
return tasks_m.size();
}
private:
// need to keep track of threads so we can join them
std::vector< std::thread > workers_m;
// the task queue
std::queue< std::function<void()> > tasks_m;
// synchronization
std::mutex queue_mutex_m;
std::condition_variable condition_m;
bool stop_m;
};
// the constructor just launches some amount of workers
inline ThreadPool::ThreadPool(size_t threads)
: stop_m(false)
{
std::thread::id id = std::this_thread::get_id();
for (size_t i = 0; i < threads; ++i)
{
workers_m.emplace_back(
[this]
{
for (;;)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex_m);
std::thread::id id1 = std::this_thread::get_id();
this->condition_m.wait(lock, [this]{ return this->stop_m || !this->tasks_m.empty(); });
std::thread::id id = std::this_thread::get_id();
if (this->stop_m && this->tasks_m.empty())
return;
task = std::move(this->tasks_m.front());
this->tasks_m.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>
{
std::thread::id id = std::this_thread::get_id();
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_m);
// don't allow enqueueing after stopping the pool
if (stop_m)
throw std::runtime_error("enqueue on stopped ThreadPool");
std::thread::id id = std::this_thread::get_id();
tasks_m.emplace([task](){ (*task)(); });
}
condition_m.notify_one();
return res;
}
// the destructor joins all threads
inline ThreadPool::~ThreadPool()
{
{
std::unique_lock<std::mutex> lock(queue_mutex_m);
stop_m = true;
}
condition_m.notify_all();
for (std::thread &worker : workers_m)
worker.join();
}
答案 0 :(得分:2)
你的enqueue
存储副本是不是要调用它们的参数?
如果您直接使用thread
,那就是问题,您可以使用引用包装器 - 将std::ref(h)
而不是h
- 传递给线程构造函数,以便通过h
作为参考。
我认为应该对你的ThreadPool
做同样的事情。 (如果这不起作用,则需要重新设计ThreadPool
以使其正常工作)
答案 1 :(得分:1)
问题详见于网址:
解决方案是替换:
pool.enqueue(&MultiThreadedUpdateOfVector::setArray, h, begin, end)
由:
pool.enqueue(&MultiThreadedUpdateOfVector::setArray, std::ref(h), begin, end)