本文介绍了调用pthread_cond_wait:随机分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我发现我的code随机产生的分段错误的错误。但我认为,我的code是pretty简单到目前为止,我不能找出其中的错误从何而来。因为它发生随机的,我认为是有某种竞争条件。我想这是所有code,可能是相关的,告诉我,如果你需要更多的:

recently, I noticed that my code randomly causes Segmentation Fault errors. But I think that my code is pretty simple so far and I cant figure out where that error comes from. Since it happens randomly, I assume that there is some kind of race-condition. I think this is all the code that might be relevant, tell me if you need more:

namespace thread {
    pthread_t terminated_thread_id, /* and others */;
    pthread_mutex_t terminate_thread = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t terminate_thread_signal = PTHREAD_COND_INITIALIZER;
    int total_thread_count = 0;
    int termination; // + sembufs

    inline void* Exit(void* value) {
    //  This must be unlocked after all join-related jobs are done
        semop(thread::termination, thread::termination_in_process, 2)
            pthread_mutex_lock(&thread::terminate_thread);
                thread::terminated_thread_id = pthread_self();
                pthread_cond_signal(&thread::terminate_thread_signal);
            pthread_mutex_unlock(&thread::terminate_thread);

        pthread_exit(value);
        return value;
    }
}
int main(int argc, const char** argv){
...
    pthread_mutex_lock(&thread::terminate_thread);
    if(0 != pthread_create(&thread::communication_handler_thread_id, NULL,    \
                           CommunicationHandler, NULL)){
        global::PrintDebug("pthread_create() failed", __FILE__, __LINE__);
    }
    /** 2 more pthread_create()-calls */
    do{
        thread::terminated_thread_id = pthread_self();
        pthread_cond_wait(&thread::terminate_thread_signal,                   \
                          &thread::terminate_thread);
        if(!pthread_equal(thread::terminated_thread_id, pthread_self())){
            pthread_join(thread::terminated_thread_id, NULL);
    ...
            semop(thread::termination, thread::termination_done, 1)
        }
    }while(thread::total_thread_count > 0);

    pthread_mutex_unlock(&thread::terminate_thread);
    return 0;
}

信号的 terminate_thread_signal 仅在螺纹::退出()功能发出的。该功能也仅称为在该用于创建线程的函数的末尾。

The signal terminate_thread_signal is only emitted in the thread::Exit() function. That function is also only called at the end of the function that is used to create the thread.

这是什么样的调试器显示调用堆栈:

This is what the debugger shows for the Call Stack:

#0 (    0xb7fe2424 in __kernel_vsyscall() (??:??)
#1 0xb7fbdfcf   __pthread_cond_wait(cond=0x80539c0, mutex=0x8053998) (pthread_cond_wait.c:153)
#2 0x804a094    main(argc=1, argv=0xbffff9c4) (/home/papergay/SeekYourCar/0.2/Server/main.cpp:121)

我已经知道的是,如果发生错误,则没有线程称为线程::退出()呢。
我还使用了一些初始化具名命名空间(如果可能有关)。
我使用code ::块作为IDE和GCC编译器为

What I already know is, if the error happens, then no thread has called thread::Exit() yet.I am also using an unnamed namespace with a few initializations (if that might be relevant).I am using Code::Blocks as IDE and GCC as compiler.

推荐答案

这是相当晚了,但我忘了将它张贴以备将来参考。
这是我如何固定它:

This is quite late, but I forgot to post it for future references.This is how I fixed it:

我升级从版本我的GCC编译器4.5.X版本来作为4.7.X以及从2.6.X到3.2.X我的内核和固定有关类的全球instanciating并通过提供静态成员变量的一些错误为了明确的构造函数允许不用初始化一个全球性的声明。
但我认为,升级GCC编译器是需要的所有东西。

I upgraded my GCC compiler from version to 4.5.X to version 4.7.X as well as my kernel from 2.6.X to 3.2.X and fixed some errors regarding the global instanciating of a class and a static member variable by providing an explicit constructor in order to allow a global declaration without initialization.But I think that upgrading the GCC compiler is all what was needed.

看起来像函数的实现是不妥当的。或有在内核code一些错误?

Looks like the implementation of the function was not proper. Or there were some errors in the kernel code?

这篇关于调用pthread_cond_wait:随机分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 14:39