简体   繁体   English

Linux中的线程同步?

[英]Thread synchronization in Linux?

I am writing a code wherein I am making my threads wait till I do a pthread_cond_broadcast. 我正在编写一个代码,其中使我的线程等待直到执行pthread_cond_broadcast。 I have three threads in this code. 我在这段代码中有三个线程。 Threads line2_thread and line3_thread, are running in the order of their priority like they are supposed to run. 线程line2_thread和line3_thread按照其优先级的顺序运行,就像它们应该运行一样。 However, my third thread doesn't even enter its thread function (line4_thread). 但是,我的第三个线程甚至没有输入其线程函数(line4_thread)。 Can anyone tell me why is my main() not being able to call my line4_thread ? 谁能告诉我为什么我的main()无法调用line4_thread吗? pthread_cond_t sstart; pthread_cond_t sstart; pthread_mutex_t sstart_mutex; pthread_mutex_t sstart_mutex;

void *l3_thread(void *arg){

pthread_mutex_lock(&sstart_mutex);
pthread_cond_wait(&sstart, &sstart_mutex);
pthread_mutex_unlock(&sstart_mutex);
/*do something*/

pthread_exit(NULL);

}

void *l2_thread(void *arg){

pthread_mutex_lock(&sstart_mutex);
pthread_cond_wait(&sstart, &sstart_mutex);
pthread_mutex_unlock(&sstart_mutex);
/*do something*/

pthread_exit(NULL);

}


void *l4_thread(void *arg){

pthread_mutex_lock(&sstart_mutex);
pthread_cond_wait(&sstart, &sstart_mutex);
pthread_mutex_unlock(&sstart_mutex);
/*do something*/

pthread_exit(NULL);

} }

int main(){

pthread_cond_init(&sstart, NULL);

//thread creation

pthread_cond_broadcast(&sstart);
pthread_cond_destroy(&sstart);
    pthread_mutex_destroy(&sstart_mutex);

return 0;
}

I think you have a few problems here. 我认为您在这里遇到一些问题。 With apologies (I'm on my phone so typing a long answer is hard) I'm just going to focus on a couple things, since it's not 100% clear to me what you're actually trying to do. 道歉(我正在打电话,所以很难输入一个很长的答案),我只专注于几件事,因为我不太清楚您实际上要做什么。

When all your threads start they all try to acquire the mutex, and only one succeeds. 当所有线程启动时,它们都尝试获取互斥量,只有一个成功。 Probably l3 but I don't think that's guaranteed here. 可能是l3,但我认为这不能保证。 It then calls the pthread_cond_wait and unlocks the mutex allowing one of the other threads to reach its pthread_cond_wait . 然后,它调用pthread_cond_wait并解锁互斥锁,从而允许其他线程之一到达其pthread_cond_wait But in the meantime. 但是与此同时。 You've allowed your main thread to call pthread_cond_broadcast , and you've taken no steps to synchronize this with the other threads. 您已经允许主线程调用pthread_cond_broadcast ,并且没有采取任何步骤将其与其他线程同步。 It may happen before the others get unblocked from waiting for the mutex, and before their wait call, so they could miss the signal and block forever. 它可能发生在其他人解除等待互斥对象的阻塞之前以及wait呼叫之前,因此他们可能会错过信号并永远阻塞。

Further, I think it's a bit sketchy to immediately call pthread_cond_destroy . 此外,我认为立即调用pthread_cond_destroy有点粗略。 Like I said there's no synchronization between your main thread and your worker threads, so it's possible you could call pthread_cond_broadcast followed by pthread_cond_destroy , so some of your threads might be calling pthread_cond_wait on an invalid condition variable and deadlock. 就像我说有你的主线程和你的工作线程之间没有同步,所以有可能你可以调用pthread_cond_broadcast其次pthread_cond_destroy ,所以你的一些线程可能会调用pthread_cond_wait上的无效状态变量和死锁。

Check the return values of pthread_cond_wait . 检查pthread_cond_wait的返回值。 If I'm right, it might return EINVAL in some cases. 如果我是对的,则在某些情况下它可能会返回EINVAL But I haven't tested this so there might be a flaw in my reasoning. 但是我尚未对此进行测试,因此我的推理可能存在缺陷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM