矢量的多线程更新
Multithreaded update of a vector
我正在尝试使用线程池更新向量,其中每个工作人员负责更新向量的特定部分。更具体地说,我将向量分成具有空交集的子集,其中集合的数量由我的计算机可用的 cpus 数量给出 (4)。
每个线程工作者将更新:
std::array<int, arraySize> vec
如果此数组在 class 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();
}
您的 enqueue
是否存储了调用参数的副本?
如果您直接使用 thread
,那将是问题所在,您将使用引用包装器——将 std::ref(h)
而不是 h
传递给线程构造函数,以便通过引用传递 h
。
我想你的 ThreadPool
也应该做同样的事情。 (如果这不起作用,则 ThreadPool
需要重新设计才能起作用)
问题在url上有详细描述:
解决方法是更换:
pool.enqueue(&MultiThreadedUpdateOfVector::setArray, h, begin, end)
作者:
pool.enqueue(&MultiThreadedUpdateOfVector::setArray, std::ref(h), begin, end)
我正在尝试使用线程池更新向量,其中每个工作人员负责更新向量的特定部分。更具体地说,我将向量分成具有空交集的子集,其中集合的数量由我的计算机可用的 cpus 数量给出 (4)。
每个线程工作者将更新:
std::array<int, arraySize> vec
如果此数组在 class 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();
}
您的 enqueue
是否存储了调用参数的副本?
如果您直接使用 thread
,那将是问题所在,您将使用引用包装器——将 std::ref(h)
而不是 h
传递给线程构造函数,以便通过引用传递 h
。
我想你的 ThreadPool
也应该做同样的事情。 (如果这不起作用,则 ThreadPool
需要重新设计才能起作用)
问题在url上有详细描述:
解决方法是更换:
pool.enqueue(&MultiThreadedUpdateOfVector::setArray, h, begin, end)
作者:
pool.enqueue(&MultiThreadedUpdateOfVector::setArray, std::ref(h), begin, end)