有条件的等待与pthread的 [英] Conditional wait with pthreads

查看:125
本文介绍了有条件的等待与pthread的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎在用一个pthreads的条件变量可能的死锁运行。

I seem to be running in to a possible deadlock with a pthreads conditional variable.

下面是code

thread function(){
    for (condition){
        do work
        /* should the thread continue? */
        if (exit == 1){
            break; /* exit for */
        } 
     } /* end for */

pthread_mutex_lock(&mtxExit);
exit = 0;
pthread_cond_signal(&condVar);
pthread_mutex_unlock(&mtxExit);
}

主要功能如下:

function main(){
    if (thread is still active){
          pthread_mutex_lock(&mtxExit);
          exit = 1;
          pthread_mutex_unlock(&mtxExit);
          } /* end if */
    while (exit == 1){
       pthread_mutex_lock(&mtxExit);
       /* check again */
       if (exit == 1)
           pthread_cond_wait(&condVar, &mtxExit);
       pthread_mutex_unlock(&mtxExit);
       }
    create new thread()
    ....
    }

在code总是卡住在cond_wait。 (

The code is always getting stuck at cond_wait. :(

编辑:

让我补充一些澄清到线程来解释我在做什么。

Let me add some clarification to the thread to explain what I am doing.

在任何时候,我只需要一个线程在运行。我有一个启动该线程,告诉它做什么,主线程继续它的工作的功能。

At any given time, I need only one thread running. I have a function that starts the thread, tells it what to do and the main thread continues it work.

下一次主线程决定它需要产生另一个线程,它必须确保是previously开始已退出该线程。我不能有两个线程在同一时间活着,它们会互相干扰。这是由设计和我的工作问题的定义。

The next time the main thread decides it needs to spawn another thread, it has to make sure the thread that was previously started has exited. I cannot have two threads alive at the same time as they will interfere with each other. This is by design and by definition of the problem I am working on.

这就是我中运行的问题。

That is where I am running in to problems.

这是我的方法:

启动线程,让它做它的工作。

Start the thread, let it do its job.

在其作业的每一步的线程检查,看它是否仍然适用。这是退出一词中的画面。主线程设置退出1,如果需要告诉线程,它不再是相关的。

the thread checks in every step of its job to see if it is still relevant. This is where "exit" comes in to picture. The main thread sets "exit" to 1, if it needs to tell the thread that it is no longer relevant.

在大多数情况下,该线程将前主线程决定产卵另一个线程退出。但我仍然需要在该线程仍然活着由主线程准备启动另一个时间的情况下因素。

In most cases, the thread will exit before the main thread decides to spawn another thread. But I still need to factor in the case that the thread is still alive by the time the main thread is ready to start another one.

所以主线程设置退出的值,并需要等待线程退出。我不想以0使用pthread_kill的信号,因为那么主线程将处于一个循环浪费CPU周期。我需要的主线程放弃控制权和睡眠/等到线程退出。

So the main thread sets the value of "exit" and needs to wait for the thread to exit. I dont want to use pthread_kill with 0 as signal because then main thread will be in a loop wasting CPU cycles. I need the main thread to relinquish control and sleep/wait till the thread exits.

因为我只需要一个线程的时候,我不需要担心扩展到更多的线程。该解决方案将永远不会有一个以上的线程。我只是需要一个可靠的机制来测试,如果我的线程还活着,如果是,则表示它退出,等待它退出,并开始下一个。

Since I only need one thread at a time, I dont need to worry about scaling to more threads. The solution will never have more than one thread. I just need a reliable mechanism to test if my thread is still alive, if it is, signal it to exit, wait for it to exit and start the next one.

从我的测试,它看起来像,主线程仍然进入即使线程可能已经退出或该信号是不获取传递到主线程在所有的条件变量。而且它永远等候在那里。并且某些情况下,在调试器我看到出口的值被设置为0和静止主线程在信号等候。似乎有一个竞争条件一些地方。

From my testing, it looks like, the main thread is still entering the conditional variable even if the thread may have exited or that the signal is not getting delivered to the main thread at all. And its waiting there forever. And is some cases, in debugger I see that the value of exit is set to 0 and still the main thread is waiting at signal. There seems to be a race condition some where.

我不是我如何设置code,现在的风扇,其过于凌乱。它唯一的一个概念证明,现在,我会很快转移到一个更好的解决方案。我的挑战是可靠的信号线程退出,等待它退出。

I am not a fan of how I set up the code right now, its too messy. Its only a proof of concept right now, I will move to a better solution soon. My challenge is to reliably signal the thread to exit, wait on it to exit.

我AP preciate你的时间。

I appreciate your time.

推荐答案

你忘了初始化条件变量?

Did you forget to initialize your condition variable?

pthread_cond_init(&condVar, NULL)

这篇关于有条件的等待与pthread的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆