【发布时间】:2016-01-10 03:15:36
【问题描述】:
下面的代码在 std::thread 类的 .join() 中引发了分段错误。但是,只有我使用 cv::fastMalloc 分配数据数组时才会发生这种情况。如果我使用 'new' 关键字或 std::malloc 函数,则不会发生错误。
我需要了解为什么会发生此错误,因为实际上我需要一个使用此函数的 cv::Mat。
int main() {
uchar* data = (uchar*) cv::fastMalloc(640);
std::atomic<bool> running(true);
std::thread thread([&] () {
while(running) {
// I'll perform some process with data here
// for now, just to illustrate, I put thread to sleep
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
});
std::this_thread::sleep_for(std::chrono::seconds(1));
running = false;
// segfault is thrown here
thread.join();
cv::fastFree(data);
return 0;
}
GDB 调用栈如下所示
#0 00429B26 _pthread_cleanup_dest () (??:??)
#1 003E32A0 ?? () (??:??)
有人知道会发生什么吗?我真的觉得这太疯狂了:S。
谢谢。
【问题讨论】:
-
您使用的是哪个版本?你是如何编译/链接的?如果这一切都很好,看起来像一个明确的错误。
-
mingw32-g++.exe -std=c++11 -Wall -fexceptions -lm main.cpp -o main.omingw32-g++.exe -o main.o -lopencv_core2410.dll.aMinGW 4.8.1 和 OpenCV 2.4.10 -
不知道 mingw,但通常当您使用 std::thread 时,您还需要链接
-lpthread。 -
是的,我已经试过了,没有效果。我也尝试设置
-pthreadcompiler 标志,但也没有用。
标签: c++ multithreading opencv c++11