线程示例,分段错误
thread example, segmentation fault
我写了一个简单的C++代码来查找向量的最小值,如下所示。它在 VC++ 和 g++ 上编译,但在后者上遇到分段错误。我无法分辨我的代码是否包含 UB 或 g++ 包含错误。有人可以识别我的代码中的任何错误吗?
段错误出现在 thread::join() 处。
一些调试信息
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) where
#0 0x0000000000000000 in ?? ()
#1 0x00000000004688f7 in std::thread::join() ()
#2 0x0000000000000000 in ?? ()
(gdb) thread
[Current thread is 1 (Thread 0x7c6880 (LWP 24015))]
这是代码
#include <iostream>
#include <random>
#include <thread>
#include <vector>
#include <algorithm>
using namespace std;
void find_min(vector<double>& x, double& min_val, int& min_id)
{
min_id = distance(x.begin(), min_element(x.begin(), x.end()));
min_val = x[min_id];
}
void find_part_min(vector<double>& x, vector<int>& min_ids, vector<double>& min_vals, int id)
{
int start_id = (x.size()*id) / min_vals.size();
int end_id = (x.size()*(id + 1)) / min_vals.size();
for (int i = start_id; i < end_id; ++i)
{
if (x[i] < min_vals[id])
{
min_ids[id] = i;
min_vals[id] = x[i];
}
}
}
int main()
{
// define variables
int Nthreads = 16;
vector<double> x(256 * 256);
int min_id = 0;
double min_val = 0;
// fill up vector with random content
mt19937 gen(0);
uniform_real_distribution<> dis(0, 1);
generate(x.begin(), x.end(), bind(dis,gen));
// find min serial
find_min(x, min_val, min_id);
cout << min_id << "t" << min_val << endl;
// initilaize variables for parallel computing
vector<double> min_vals(Nthreads, numeric_limits<double>::infinity());
vector<int> min_ids(Nthreads, -1);
vector<thread> myThreads;
for (int id = 0; id < Nthreads; ++id) // define each thread
{
thread myThread(find_part_min, ref(x), ref(min_ids), ref(min_vals), id);
myThreads.push_back(move(myThread));
}
for (int id = 0; id < Nthreads; ++id)
myThreads[id].join(); // part-calculations are finished
// merging the results together
min_val = numeric_limits<double>::infinity();
min_id = -1;
for (int i = 0; i < Nthreads; ++i)
{
if (min_vals[i] < min_val)
{
min_val = min_vals[i];
min_id = min_ids[i];
}
}
cout << min_id << "t" << min_val << endl;
return 0;
}
我查看了 Makefile,-static
在没有 -whole-archive
的情况下使用,这导致 g++ 下的问题 https://gcc.gnu.org/ml/gcc-help/2010-05/msg00029.html
它告诉,如果在没有__thread
支持的情况下配置libstdc++并且针对-lpthread
的链接是静态的,则这可能是由于libstdc++错误而发生的。
您应该在 GCC (g++) 的每个编译阶段使用 -pthread
作为选项,而不是针对 -lpthread
进行链接。
比使用该标志的简单链接要多。
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)
- 分段错误 - 读取初始化指针的数组
- 如何摆脱C ++中的分段错误错误?
- 使用 CTYPE 时出现分段错误
- 为什么代码给出分段错误?