C++ 为调试目的创建 SIGSEGV
我正在开发一个无锁共享变量类,我希望能够生成一个 SIGSEGV 错误,以查看我的实现是否按计划工作.我尝试创建一个修改指针并读取它 100 次的函数.然后我在两个线程中调用这个函数并让线程在我的程序中无限运行.这不会产生我想要的错误.我该怎么做呢?
I am working on a lock-free shared variable class, and I want to be able to generate a SIGSEGV fault to see if my implementation works as I planned. I've tried creating a function that modifies a pointer and read it 100 times. I then call this function in both threads and have the threads run infinitely within my program. This doesn't generate the error I want. How should I go about doing this?
编辑我根本不处理段错误,但是如果我删除锁,它们会在我的程序中生成.我想使用无锁设计,因此我创建了一个使用 CAS 保持无锁的共享变量类.有没有办法让我有一段代码会生成段错误,以便我可以使用我的类来测试它是否解决了问题?
edit I don't handle segfaults at all, but they are generated in my program if I remove locks. I want to use a lock-less design, therefore i created a shared variable class that uses CAS to remain lockless. Is there are way that I can have a piece of code that will generate segfaults, so that i can use my class to test that it fixes the problem?
推荐答案
#include <signal.h>
raise(SIGSEGV);
将引发适当的信号.
相关文章