C 独立线程错误
C++ detached thread bug
花了一天之后试图解决一个神秘的错误,我寻求您的帮助。
当我运行代码时,您是否了解为什么要复制" Hello 3"输出?
#include <iostream>
#include <future>
#include <string>
#include <functional>
#include <type_traits>
#include <unistd.h>
template <class Fn, class... Args>
inline decltype(auto) runTerminateOnException(Fn&& fn, Args&&... args) {
try {
return std::invoke(std::forward<Fn>(fn), std::forward<Args>(args)...);
} catch (...) {
std::terminate();
}
}
struct RunTerminateOnExceptionInvoker {
template <class Fn, class... Args>
decltype(auto) operator()(Fn&& fn, Args&&... args) const {
return runTerminateOnException(std::forward<Fn>(fn), std::forward<Args>(args)...);
}
};
template <class Fn, class... Args>
inline decltype(auto) runAsyncTerminateOnException(Fn&& fn, Args&&... args) {
usleep(1000);
return std::async(std::launch::async, RunTerminateOnExceptionInvoker(), std::forward<Fn>(fn), std::forward<Args>(args)...);
}
template <class Fn, class... Args>
inline void runOnDetachedThreadTerminateOnException(Fn&& fn, Args&&... args) {
usleep(1000000);
std::thread(RunTerminateOnExceptionInvoker(), std::forward<Fn>(fn), std::forward<Args>(args)...).detach();
}
struct A {
template <class T>
static void g(double x, const std::shared_ptr<std::string> &s) {
T t{};
std::cout << "g() : x = " << x << ", *s = " << *s << ", t = " << t << std::endl;
}
static void f(double x, std::shared_ptr<std::string> &s1, std::shared_ptr<std::string> &s2, std::shared_ptr<std::string> &s3) {
printf("Coucou 1n");
runAsyncTerminateOnException(g<double>, x, s1); // Working
printf("Coucou 2n");
auto future1 = runAsyncTerminateOnException(g<double>, x, s2); // Working
printf("Coucou 3n");
runOnDetachedThreadTerminateOnException(g<double>, x, s3); // Working
}
};
int main() {
auto s1 = std::make_shared<std::string>("Hello 1");
auto s2 = std::make_shared<std::string>("Hello 2");
auto s3 = std::make_shared<std::string>("Hello 3");
A::f(10., s1, s2, s3);
printf("Coucou 4n");
return 0;
}
输出:
Coucou 1
g() : x = 10, *s = Hello 1, t = 0
Coucou 2
Coucou 3
g() : x = 10, *s = Hello 2, t = 0
Coucou 4
g() : x = 10, *s = Hello 3, t = 0
g() : x = 10, *s = Hello 3, t = 0
实时运行
谢谢
您在这里有未定义的行为。由于线程已脱离,因此您不能保证*s3
的生存时间与独立线程一样长。main
在您独立的工作人员线程执行之前结束,所有这些字符串都不在范围。
您必须按价值通过共享指针才能真正获得其参考计数能力。目前,对于每个测试,您只是通过参考来传递一个shared_ptr
实例,这并不是特别有用。
如果您按值将它们传递到A::f
中,则情况按预期工作。
我不能完全弄清楚编译器在您的情况下所做的魔术,并对这个具体的结果感到有些惊讶,但是幸运的是,我只能说"它是UB",然后将东西留在那里;(
坦率地说,我什至不确定让独立线程越过main
的末端是否有效,但是我会让别人解决这个方面。
相关文章:
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++使用params创建线程函数会导致转换错误
- 是什么导致了Unity 3D中的"错误线程异常"?
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 尝试从头开始实现Leetcode的FizzBuzz多线程问题。收到"libc++abi.dylib: terminating"错误
- 线程时访问静态映射时出现隔离错误
- 多线程减慢程序速度:无错误共享,无互斥锁,无缓存未命中,无小工作量
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- C++多线程程序:变量定义为类成员的隔离错误
- 当 2 个线程共享同一物理内核时,具有错误共享的易失性增量在发布中的运行速度比在调试中慢
- 线程 std::调用未知类型,无法专门化函数错误
- 停止 winapi 线程时出现错误行为
- C++线程错误
- C++线程错误:没有名为"类型"MINGW 的类型
- 可中断线程类 C++11 - 遇到错误?
- 分离的线程:在繁忙时销毁互斥锁 错误C++
- 为同一向量C++运行多个线程时出现分段错误
- 查找最低值.功能.错误:线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x7fff5fc89000)
- MINGW错误:“线程”不是“ STD”的成员