分段故障(岩芯倾倒)与c++;11线 我是一个C++新手,在玩C++ 11的线程时,我得到了一个“分割错误(内核转储)”。我修改了一段我以前写的好代码,结果出错了。我修改的部分是 mutex m; auto thread_f=[&](int i) { for(int j=i; j<interval.size(); j+=Threads_Number) { vector<Permutation >::iterator it1=(interval.begin()+j); for(vector<Permutation >::iterator it2=it1; it2!=interval.end(); it2++) { if(!(*it1<*it2)) continue; IntervalToCheck x(*it1,*it2); m.lock(); cout<<x; f<<x; m.unlock(); } } }; vector<thread> threads; threads.clear(); for(int i=0; i<Threads_Number; i++) threads.push_back(thread(thread_f,i)); for(auto& th:threads) th.join();

分段故障(岩芯倾倒)与c++;11线 我是一个C++新手,在玩C++ 11的线程时,我得到了一个“分割错误(内核转储)”。我修改了一段我以前写的好代码,结果出错了。我修改的部分是 mutex m; auto thread_f=[&](int i) { for(int j=i; j<interval.size(); j+=Threads_Number) { vector<Permutation >::iterator it1=(interval.begin()+j); for(vector<Permutation >::iterator it2=it1; it2!=interval.end(); it2++) { if(!(*it1<*it2)) continue; IntervalToCheck x(*it1,*it2); m.lock(); cout<<x; f<<x; m.unlock(); } } }; vector<thread> threads; threads.clear(); for(int i=0; i<Threads_Number; i++) threads.push_back(thread(thread_f,i)); for(auto& th:threads) th.join();,c++,multithreading,gcc,c++11,C++,Multithreading,Gcc,C++11,我使用: g++ -c main.cpp --std=c++11 g++ *.o -o work -lcln -lginac -pthread 编译我的代码。谢谢你的关注,很抱歉我的英语很差 这似乎是因为我在类IntervalToCheck中使用GiNaC,而且它不是线程安全的(正如我在谷歌上搜索GiNaC和线程安全),因为我得到了这样的信息 Program received signal SIGSEGV, Segmentation fault. [Switching to Thread

我使用:

g++ -c main.cpp --std=c++11

g++ *.o -o work -lcln -lginac -pthread 
编译我的代码。谢谢你的关注,很抱歉我的英语很差

这似乎是因为我在类IntervalToCheck中使用GiNaC,而且它不是线程安全的(正如我在谷歌上搜索GiNaC和线程安全),因为我得到了这样的信息

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff5340700 (LWP 3125)]
0x00000000004097cf in GiNaC::ptr<GiNaC::basic>::operator= (this=0x7fffec000cd0,   other=...) at /usr/include/ginac/ptr.h:88
88              delete p;
程序接收信号SIGSEGV,分段故障。
[切换到线程0x7ffff5340700(LWP 3125)]
在/usr/include/GiNaC/ptr.h:88处,GiNaC::ptr::operator=(this=0x7fffec000cd0,other=…)中的0x00000000004097cf
88删除p;

根据n.m.的建议,来自gdb。也许问题出在吉纳克身上。如果有人能提供一个处理表达式的开放工具,那将是非常棒的。thx用于阅读。

如果您查看分段错误来源的源代码,您可以看到它来自GiNaC的a实现。据我所知,问题在于,当另一个线程仍在使用指针时,指针可能会被过早删除。这是因为计数器上的递增和递减操作与线程访问无关。因此,您正试图访问已删除的内存,这是未定义的行为(因此是可分段的)

事实上,这个问题似乎在过去就已经出现了

因此,您不能像维护人员所期望的那样从多个线程使用GiNaC

从理论上讲,也许您可以用类似于
std::shared_ptr
的东西来代替
ginac::ptr
,这保证了它的引用计数机制没有竞争条件。因此,如果您可以将内部计数器类型替换为
std::atomic
或类似的内容,或者您可以使用另一个具有原子引用计数的实现,那么该程序可能会工作


至于您编写的实际代码,它似乎可以正常工作。虽然打印语句可以按顺序执行和交错,但它们似乎在一个关键块中被正确地锁定,不过在C++中使用更通用的RAII增强<代码> STD::SCONDEXYOLD 更好。code>gdb您的程序名应该可以让您开始使用。对于
clear()
一个空向量是没有意义的。您不应该调用
m.lock()
m.unlock()
,而应该使用标准锁类型之一
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff5340700 (LWP 3125)]
0x00000000004097cf in GiNaC::ptr<GiNaC::basic>::operator= (this=0x7fffec000cd0,   other=...) at /usr/include/ginac/ptr.h:88
88              delete p;