snk*_*snk 1 c++ vector std c++11
我有一些std :: thread的类warper.这是构造函数:
template <typename Function, typename... Args>
InterruptibleThread(Function&& fun, Args&&... args)
{
_thread = std::thread([](std::atomic_bool * f, Function&& function, Args&&... arguments)
{
_flag_ref = f;
(function)(std::forward<Args>(arguments)...);
},
&_flag,
std::forward<Function>(fun)
, std::forward<Args>(args)...
);
}
Run Code Online (Sandbox Code Playgroud)
然后我正在使用它(例子):InterruptibleThread(&SourceImageList :: StartFrameProcessingStatic,this,std :: ref(it))
编译器成功构建此代码.但现在我想制作一个这样的对象的矢量:
std::vector<InterruptibleThread> grp;
Run Code Online (Sandbox Code Playgroud)
我想在堆栈上分配它,所以我正在做的是:
grp.emplace_back(&SourceImageList::StartFrameProcessingStatic, this, std::ref(it));
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
C2064 term does not evaluate to a function taking 0 arguments
Run Code Online (Sandbox Code Playgroud)
以下是编译器验证的选项:
1) grp.push_back(new InterruptibleThread(&SourceImageList::StartFrameProcessingStatic, this, std::ref(it)));
2) grp.push_back(InterruptibleThread(&SourceImageList::StartFrameProcessingStatic, this, std::ref(it)));
Run Code Online (Sandbox Code Playgroud)
但第一个是在堆上分配一个对象,所以我需要手动释放它,第二个是对象的副本.
我可以emplace_back
在这里使用(编译器是MSVC 2015更新3)吗?
更新
好的,我根据答案做了一些修复.这是这个类的最终版本:
#pragma once
#include <exception>
#include <atomic>
#include <thread>
#include <future>
#include <windows.h>
// Synopsis
class InterruptThreadException;
class InterruptibleThread;
// Interrupt exception
class InterruptThreadException : public virtual std::exception {
public:
virtual char const* what() const override { return "interrupt"; }
}; // class InterruptThreadException
// Interruptible thread
class InterruptibleThread {
public:
static void InterruptionPoint() noexcept(false) {
if (!InterruptibleThread::_flag_ref) { return; }
if (!InterruptibleThread::_flag_ref->load()) { return; }
throw InterruptThreadException();
} // check_for_interrupt
template <typename Function>
InterruptibleThread(Function&& fun) :
_thread([this, fun = std::move(std::forward<Function>(fun))]
{
_flag_ref = _flag.get();
fun();
})
{}
InterruptibleThread(InterruptibleThread&&) = default;
InterruptibleThread(const InterruptibleThread&) = delete;
bool Interrupting() const { return _flag->load(); }
void Interrupt() { _flag->store(true); }
void Join()
{
_thread.join();
}
bool TimedJoin(int msec)
{
return (std::async([=]() {Join(); }).wait_for(std::chrono::milliseconds(msec)) != std::future_status::timeout);
}
bool Joinable()
{
return _thread.joinable();
}
void Terminate()
{
TerminateThread(_thread.native_handle(), -1);
}
~InterruptibleThread()
{
if (_flag.get() != nullptr)
{
*_flag = false;
Interrupt();
}
if (_thread.joinable())
_thread.join()
}
private:
static thread_local std::atomic_bool* _flag_ref;
std::unique_ptr<std::atomic_bool> _flag = std::make_unique<std::atomic_bool>();
std::thread _thread;
};
Run Code Online (Sandbox Code Playgroud)
和使用的例子:
std::vector<InterruptibleThread> grp;
for (auto it : _sourceImages)
grp.emplace_back([this, it] {
it->StartFrameProcessing();
it->SetImageDelay(const_cast<EngineConfig*>(GetConfig())->ImageDelay);
});
Run Code Online (Sandbox Code Playgroud)
您可以使代码现代化并将lambda传递给InterruptibleThread
而不是传递函数及其参数(即bind-style).
#include <atomic>
#include <iostream>
#include <thread>
#include <vector>
struct InterruptibleThread
{
std::thread _thread;
template <typename Function>
InterruptibleThread(Function&& fun)
: _thread(std::forward<Function>(fun))
{
}
};
struct Test
{
std::vector<InterruptibleThread> grp;
void test(int x) {
grp.emplace_back([this, x]{ t1(x); }); // <==== HERE
}
void t1(int x) {
std::cout << x << "\n";
}
};
int main()
{
Test t;
t.test(5);
t.grp[0]._thread.join();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
823 次 |
最近记录: |