c++ - Pthread 条件变量不可预测的结果

标签 c++ linux g++ pthreads

首先很抱歉提出一个很长的问题,但请保持耐心 :)。

作为我的应用程序的一部分,我使用线程来读取内存映射文件的不同部分,因为映射文件的前半部分由主线程读取和处理,而映射文件的后半部分由另一个线程读取和处理.

我正在使用条件变量,以确保第二个线程一直等到主线程在结构中填充文件相关值。

使用这种方法,我有时会正确填充文件大小,有时却无法正确显示。

这是适用的代码:

void * thread_proc(void * arg){

    struct thread_related_data * dat_str = (struct thread_related_data *) arg;

    //pthread_detach(pthread_self());

    pthread_mutex_lock(&(dat_str->mutex));

    while(dat_str->thread_indicator == 0 ){
        pthread_cond_wait(&(dat_str->cond),&(dat_str->mutex));
    }

    if(dat_str->thread_indicator == 1){   // Start data processing

// start processing bottom half of the mapped file and insert data into &dat_str->thread_proc_data

        pthread_mutex_unlock(&(dat_str->mutex));
        printf ("Got conditioned\n");

        int bytes = dat_str->file_size - dat_str->start_bytes; // bytes should have almost half of the length of the file in bytes, but it does not always print the value correctly in the next statement .

        printf(" Bytes :: %d\n", dat_str->start_bytes); // Sometimes this line gets printed twice ..dont know how !!!

        char *start;
        int i = 0;
        while(i <= bytes){
            start = dat_str->file;
            while(*(dat_str->file) != '\n'){
                //printf file++;
            }

            line_proc(start,static_cast(dat_str->file - start - 1),dat_str->phrase);
            dat_str->file++;
            i++;
        }

    }
    return NULL;
}


void inputFile::start_processing(common& com , searchable& phrases){
    pthread_create(&thread1,NULL,thread_proc,&trd);
    //trd.fil_obj = this;
    // Set the char pointer for the thread

    char * temp = file; // pointer to memory mapped file
    temp += file_size/2;
    //cout.setf(std::ios::unitbuf);
    int bytes = temp - file;

    while(*temp != '\n'){
        bytes++;
        temp++;
    }

    if(*temp == '\n'){
        temp++;

        pthread_mutex_lock(&(trd.mutex));
        trd.thread_indicator = 1;         // signalling variable
        trd.file = temp;
        trd.phrase = phrases.text_to_search[0];
        trd.start_bytes = bytes + 1;     // the start pointer of the mapped file
                                                 // for the second thread.
                                                 // i.e second thread will start processing                         from this pointer
        pthread_cond_signal(&(trd.cond));
        pthread_mutex_unlock(&(trd.mutex));

        // Now process half of the file and put the results in record vector

        temp--; // this thread will process upto '\n' first half of the file
         }
}

请让我知道我在哪里犯了逻辑错误或者我是否以错误的方式实现了条件变量。

此外,我已将互斥锁和条件变量分别初始化为 pthread_mutex_init(&(trd.mutex),NULL) 和 pthread_cond_init(&(trd.cond),NULL) 而不是 PTHREAD_MUTEX_INITIALIZER 和 PTHREAD_COND_INITIALIZER。这会是我的问题的原因吗?

谢谢。

最佳答案

您对 sleep 条件变量的使用略有偏差,通常的方法是:

while(!condition)
{
    pthread_cond_wait(...);
}

这处理虚假唤醒的情况(可能发生)。您当前的代码未检查任何条件,但可能应更改为如下所示:

while (dat_str->thread_indicator != 1)
{
    pthread_cond_wait(&(dat_str->cond),&(dat_str->mutex));
}

关于c++ - Pthread 条件变量不可预测的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6737678/

相关文章:

c++ - 在 C++ 中求值是什么意思?

java - Jprofiler Agent安装报错Centos 7

c++ - 带有前向声明的嵌套类在 clang++ 中导致错误,但在 g++ 上没有警告地通过

c++ - fnaf奇怪的表现

gcc - 迄今为止最稳定的 gcc/g++ 版本

在 Windows 上运行并生成 Linux 代码的 C++ 编译器

c++ - 尝试将 std::sort() 与自定义对象一起使用

c++ - 在计算着色器中计算帧缓冲区的颜色直方图

linux - 邮件 : Options MUST PRECEDE persons

c - 将 Ruby 的事件循环集成到 C 扩展中