c++11多线程内存错误
c++11 multithreading memory error
我有以下代码:
somefunc(string s, semaphore* sem) {
//some functionality
sem->signal();
}
int main() {
int num = 0;
semaphore sem(0);
vector<string> arr;
for (string& s : arr) {
++num;
thread(somefunc, s, &sem).detach();
}
for (int i = 0; i < num; i++)
sem.wait();
}
我在thread()
所在的行上的std::string
分配中收到SIGSEGV
错误。这个代码有问题吗?semaphore
是一个使用互斥和条件变量的自定义类。
如果没有看到真实的代码,很难判断,但我的猜测是,当字符串引用s
被复制到thread
中时,它不再有效,因为主线程已经退出了包含arr
的作用域。
在valgrind或类似的内存检查器下运行代码应该有助于诊断问题。
相关文章:
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 内存错误低于在C++年实现埃拉托色尼筛分时的预期
- 共享内存:MapViewOfFile 返回错误 5
- 将 vector<vector<int>> 传递到函数中会产生内存错误
- 释放动态内存时C++错误
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- 复制内存给出分段错误
- C++程序错误:malloc():内存损坏
- 在我的以下代码中获取 MLE(内存限制错误).尝试解决 ROUND C 2019(问题 A-摆动行走)启动问题
- 为什么删除分配的阵列会导致内存错误?
- 需要找到3个小错误-内存错误
- 错误:内存位置的 std::length_error
- 修复了瓦尔格林德错误内存泄漏的错误
- 错误:“内存集”未在此范围内声明
- OpenCV 2.4.5运行时错误内存
- OpenCV错误:内存不足
- STL映射,设置错误:内存超出分配块的末尾
- GLUT:致命错误:内存不足
- MPI错误:内存不足-有哪些解决方案选项