代码在没有使用openmp的情况下工作正常。我使用了私有和共享,但它仍然在我的结构SinRes
被定义的行上给出了SIGSEGV。
#pragma omp parallel for private(row, dt1, dt1s, dtmp, dat1, atur) shared(turVec)
for(row=0; row<ndicts; ++row)
{
dt1 = kI(keys)[row];
dt1s = to_string(dj(dt1));
dtmp = dt1s.substr(0, 4) + "." + dt1s.substr(4, 2) + "." + dt1s.substr(6, 2);
dat1 = kK(data)[row];
dat1s = kK(dat1->k)[1];
atur = breakLogic(dat1s, mpar, dtmp);
///free(atur);
turVec.push_back(atur);
}
错误消息是
(gdb) run 5 x5.csv
Starting program: /root/cwork/ompTur 5 x5.csv
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x2aaaaaacd000
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x2aaaac2e1700 (LWP 17500)]
[New Thread 0x2aaaac4e2700 (LWP 17501)]
[New Thread 0x2aaaac6e3700 (LWP 17502)]
[New Thread 0x2aaaad1e9700 (LWP 17503)]
[New Thread 0x2aaaad3ea700 (LWP 17504)]
[New Thread 0x2aaaad5eb700 (LWP 17505)]
[New Thread 0x2aaaad7ec700 (LWP 17506)]
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x2aaaad7ec700 (LWP 17506)]
0x0000000000407a7a in sinRes::sinRes (this=0x2aaab401f430) at ompTur.cpp:33
33 struct sinRes {
(gdb)
我是openmp的新手。我是否需要使用critical
等其他任何构造?
SinRes的代码只是
struct sinRes {
double pnl, numTrds, firstVar;
string dt;
};
答案 0 :(得分:1)
标准C ++容器类在写入操作方面不是线程安全的。必须使用明确的同步访问,例如:
#pragma omp critical
turVec.push_back(atur);
operator[]
在必要时不会扩展向量对象的内部存储,因此允许并发访问。
答案 1 :(得分:0)
您似乎将std::string
类型的变量声明为omp private
和omp shared
。它可能无法工作,因为std::string
使用动态内存分配,并且不是线程安全的。
Is std::string thead-safe with gcc 4.3?
您可能需要在for循环中手动定义thread-local std :: string,并避免使用std::string::push_back()
,因为它涉及竞争条件。