- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 Visual Studio CTP 14 基于 C++11 中的标准库实现一个具有可中断线程的并发库,例如 boost 和 Java 中的线程。
经过一些重构后,我在 std::thread 的析构函数中遇到了崩溃(有时试图取消引用空指针,有时是“调试错误!R6010”,有时根本没有崩溃)。在剥离大量代码以找到问题之后,我无法进一步了解问题是出在我的代码上还是可能是编译器错误。
即使在剥离代码之后,重现问题仍然需要很多,所以请耐心等待 :)
部分线程包装器实现:
class InterruptException : public std::exception
{
public:
const char* what() const override {return "InterruptException";}
};
class Thread
{
public:
Thread(const Thread&) = delete;
Thread& operator=(const Thread&) = delete;
Thread();
template <typename Callable>
Thread(Callable&& action);
Thread(Thread&& other);
~Thread();
Thread& operator=(Thread&& other);
// Sets interruption flag and call notifyAll for current condition if any.
void interrupt();
// Throws interrupted exception if current thread interruption flag is set.
// This also resets the interruption flag.
static void interruptionPoint();
static void interruptCurrent();
static void setCondition(std::condition_variable* cond);
static Thread* currentThread(Thread* setter = nullptr);
private:
std::atomic<bool> _interruptionFlag;
std::atomic<std::condition_variable*> _currentCondition;
std::thread _stdThread;
};
template <typename Callable>
Thread::Thread(Callable&& action)
: _stdThread(),
_interruptionFlag(false),
_currentCondition(nullptr)
{
_stdThread = std::thread(
[this, runner = std::move(action)]
{
currentThread(this);
try
{
runner();
}
catch (InterruptException&)
{
// Normal exit.
}
catch (...)
{
// Removed logging calls.
}
}
);
}
Thread::~Thread()
{
// Block at thread destruction, no detached thread support.
if (_stdThread.joinable())
{
interrupt();
_stdThread.join();
}
}
// (More code if requested.)
(请注意,为清楚起见,上面的代码缺少部分)
我已经为 Thread 类编写了多个测试,它似乎可以像预期的那样工作。下一部分是任务运行器的精简版本。
template <typename Runner>
class StrippedSystem
{
public:
template <typename... CtorArgs>
StrippedSystem(CtorArgs&&... args);
StrippedSystem(const StrippedSystem&) = delete;
StrippedSystem(StrippedSystem&&) = delete;
// ...
private:
template <typename... CtorArgs>
Thread createRunnerThread(CtorArgs&&... args);
std::mutex _mutex;
std::condition_variable _cond;
Thread _runner;
};
template <typename Runner>
template <typename... CtorArgs>
StrippedSystem<Runner>::StrippedSystem(CtorArgs&&... args)
{
_runner = createRunnerThread(std::forward<CtorArgs>(args)...);
}
template <typename Runner>
template <typename... CtorArgs>
Thread StrippedSystem<Runner>::createRunnerThread(CtorArgs&&... args)
{
auto runnerProc =
[&]
{
Thread::setCondition(&_cond);
try
{
std::unique_lock<std::mutex> lock(_mutex);
_cond.wait(
lock,
[&]
{
Thread::interruptionPoint();
return false;
}
);
}
catch (...)
{
Thread::setCondition(nullptr);
std::rethrow_exception(std::current_exception());
}
};
return Thread(runnerProc);
}
精简后的 StrippedSystem 的代码非常简单。它创建一个等待直到被中断的线程。当调用 Thread 的析构函数时,会设置中断标志并通知条件。然后线程运行到一个中断点(在 _cond.wait lambda 内部),该中断点抛出一个在线程包装器中捕获的 InterruptionException 并且线程正常退出。
现在是让一切崩溃的代码:
struct ConstructedRunner
{
ConstructedRunner(int a, std::string b)
: i(a), j(b)
{
}
int i;
std::string j;
};
int main(int argc, char* argv[])
{
{
StrippedSystem<ConstructedRunner> testSys2(1, "foobar");
}
return 0;
}
如上所述,崩溃可能是“调试错误!已调用中止”、访问冲突(尝试取消引用空指针)或在某些情况下根本没有崩溃。
经过一些故障排除后,我发现以下不会崩溃:
struct DummyRunner
{
};
int main(int argc, char* argv[])
{
{
StrippedSystem<DummyRunner> testSys1;
}
return 0;
}
经过更多的故障排除后,我发现在系统构造函数中替换
_runner = createRunnerThread(std::forward<CtorArgs>(args)...);
与
_runner = Thread(
[&]
{
Thread::setCondition(&_cond);
try
{
std::unique_lock<std::mutex> lock(_mutex);
_cond.wait(
lock,
[&]
{
Thread::interruptionPoint();
return false;
}
);
}
catch (...)
{
Thread::setCondition(nullptr);
std::rethrow_exception(std::current_exception());
}
}
);
还修复了崩溃。 即使没有使用转发的参数。
这对我来说毫无意义,因为运行的代码应该是相同的。这是编译器问题还是我做错了什么导致了一些奇怪的并发问题?
使用 Visual Studio CTP 14 for Windows 7 在 Debug模式下构建。
最佳答案
经过一些帮助,我解决了错误。
问题出在 Thread
模板构造函数中的 currentThread(this)
。该函数将线程指针设置为线程局部静态变量,稍后读取该变量以获取中断标志和当前条件(如果有)。问题是 this
指针指的是一个临时对象。这可以在 _runner = Thread(...)
中看到,其中临时 Thread
对象位于右侧并被设置为线程本地数据。线程移动后数据未更新。
在 Thread
的析构函数中,interrupt
检查线程局部标志,这就是析构函数崩溃的原因。
关于c++ - 使用 VS CTP 14 在 std::thread 析构函数上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25826305/
我正在开发一个小型图书馆,我需要做的一件事是让访问者访问一些数据并返回结果。 在一些较旧的 C++ 代码中,访问者需要声明一个 typedef return_type .例如,boost::stati
我正在尝试使用std:map类型的键和值制作std::any Visual Studio 2017 std::map m("lastname", "Ivanov"); std::cout (m["la
我已经在 C++ 的 map 中声明了一个集合为 std::map> .如何循环访问或打印设定值? 最佳答案 如果你知道如何迭代 std::map或 std::set单独地,您应该可以毫无问题地组合迭
如何循环? 我已经试过了: //----- code std::vector >::iterator it; for ( it = users.begin(); it != users.end();
我有两个用例。 A.我想同步访问两个线程的队列。 B.我想同步两个线程对队列的访问并使用条件变量,因为其中一个线程将等待另一个线程将内容存储到队列中。 对于用例 A,我看到了使用 std::lock_
我正在查看这两种类型特征的文档,但不确定有什么区别。我不是语言律师,但据我所知,它们都适用于“memcpy-able”类型。 它们可以互换使用吗? 最佳答案 不,这些术语不能互换使用。这两个术语都表示
我有以下测试代码,其中有一个参数 fS,它是 ofstream 的容器: #include #include #include #include int
这是这个问题的延续 c++ function ptr in unorderer_map, compile time error 我试图使用 std::function 而不是函数指针,并且只有当函数是
std::unordered_map str_bool_map = { {"a", true}, {"b", false}, {"c", true} }; 我们可以在此映射上使
我有以下对象 std::vector> vectorList; 然后我添加到这个使用 std::vector vec_tmp; vec_tmp.push_back(strDRG); vec_tmp.p
为什么 std::initializer_list不支持std::get<> , std::tuple_size和 std::tuple_element ?在constexpr中用得很多现在的表达式,
我有一个像这样定义的变量 auto drum = std::make_tuple ( std::make_tuple ( 0.3f , Ex
假设我有一个私有(private)std::map在我的类(class)里std::map 。我怎样才能将其转换为std::map返回给用户?我想要下面的原型(prototype) const std
假设我有一个私有(private)std::map在我的类(class)里std::map 。我怎样才能将其转换为std::map返回给用户?我想要下面的原型(prototype) const std
问题 我正在尝试将 lambda 闭包传递给 std::thread,它使用任意封闭参数调用任意封闭函数。 template std::thread timed_thread(Function&& f
我想创建一个模板类,可以容纳容器和容器的任意组合。例如,std::vector或 std::map ,例如。 我尝试了很多组合,但我必须承认模板的复杂性让我不知所措。我编译的关闭是这样的: templ
我有一个 std::vector>我将其分配给相同类型的第二个 vector 。 我收到这个编译器错误: /opt/gcc-8.2.0/include/c++/8.2.0/bits/stl_algob
有时候,我们有一个工厂可以生成一个 std::unique_ptr vector ,后来我们想在类/线程/你命名的之间共享这些指针。因此,最好改用 std::shared_ptr 。当然有一种方法可以
这个问题在这里已经有了答案: Sorting a vector of custom objects (14 个答案) 关闭 6 年前。 我创建了一个 vector vector ,我想根据我定义的参
我有三个类(class)成员: public: std::vector > getObjects(); std::vector > getObjects() const; privat
我是一名优秀的程序员,十分优秀!