问题描述
我工作的一个项目,并试图用调用pthread_cond_wait()
和调用pthread_cond_signal()
来同步两个线程
I am working on a project and trying to use pthread_cond_wait()
and pthread_cond_signal()
to synchronize two threads.
我的code看起来是这样的:
My code looks something like this:
pthread_mutex_t lock_it = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t write_it = PTHREAD_COND_INITIALIZER;
int main(int argc, char**argv)
{
pthread_t t_send_segments, t_recv_acks;
pthread_create(&t_send_segments, NULL, send_segments, (void*)NULL);
pthread_create(&t_recv_acks, NULL, recv_acks, (void*)NULL);
pthread_join(t_recv_acks, (void**)NULL);
pthread_mutex_destroy(&lock_it);
pthread_cond_destroy(&write_it);
}
void* send_segments(void *v) {
for(;;) {
pthread_mutex_lock(&lock_it);
printf("s1\n");
printf("s2\n");
pthread_cond_wait(&write_it, &lock_it);
printf("s3\n");
printf("s4\n");
printf("s5\n");
pthread_mutex_unlock(&lock_it);
}
return 0;
}
void* recv_acks(void *v) {
for(;;) {
pthread_mutex_lock(&lock_it);
printf("r1\n");
pthread_cond_signal(&write_it);
printf("r2\n");
pthread_mutex_unlock(&lock_it);
}
return 0;
}
预期的输出是:
s1
s2
r1
s3
s4
s5
s1
s2
r2
r1
s3
s4
s5
(etc)
我的输出不遵循此模式在所有。显然,我有一个逻辑错误的地方,但我不理解的地方。为什么没有 recv_acks()
线程的总是的收益率,当它击中了调用pthread_cond_signal()
- 因为调用pthread_cond_wait()
总是先执行该 cond_wait()和(因为我在其中创建线程的顺序)
总是因为执行其在临界区?
My output doesn't follow this pattern at all. Clearly I have a logic error somewhere, but I'm not understanding where. Why doesn't the recv_acks()
thread always yield when it hits the pthread_cond_signal()
- since the pthread_cond_wait()
always executes first (because of the order in which I create the threads) and the cond_wait()
always executes since its in the critical section?
推荐答案
的调用pthread_cond_signal
功能不会导致当前线程产生,并且不释放互斥锁。它所做的就是重新启动一个线程已经通过调用pthread_cond_wait
暂停本身的条件。这只是意味着唤醒线程可用于调度,它不会导致它立即执行。线程调度器将在未来某个时候安排它。
The pthread_cond_signal
function does not cause the current thread to yield and does not release the mutex. All it does is restart one thread that has suspended itself on the condition via pthread_cond_wait
. This just means the awakened thread is available for scheduling, it doesn't cause it to execute immediately. The thread-scheduler will schedule it sometime in the future.
此外,仅仅因为S-线程已经觉醒,并争夺互斥,这并不意味着它会得到下一个互斥。互斥不一定公平已经要求它的所有线程。按照 pthread_mutex
手册页:的pthread_mutex_lock
锁定特定互斥如果互斥锁当前未锁,就变成锁定。并通过调用线程拥有,而的pthread_mutex_lock
立即返回。因此,R-线程可以在其多次循环旋转,愉快地解锁并通过调度被交换之前重新锁定互斥几次。这意味着S-线程只会在互斥得到一个机会,如果调度发生过程中,它已经发布了互斥的短暂时间中断R-线程。
Also, just because the s-thread has been awakened and is contending for the mutex, that doesn't mean it's going to get the mutex next. Mutexes are not necessarily fair to all threads that have requested it. According to the pthread_mutex
man page: "pthread_mutex_lock
locks the given mutex. If the mutex is currently unlocked, it becomes locked and owned by the calling thread, and pthread_mutex_lock
returns immediately." So the r-thread can spin in its loop several times, happily unlocking and relocking the mutex several times before being swapped out by the scheduler. This means the s-thread will only get a chance at the mutex if the scheduler happens to interrupt the r-thread during the brief time in which it has released the mutex.
要达到你想要的输出,两个线程都需要控制它们的执行有一个条件,并暂停自己之前的信号对方。然而,这可能是也可能不是你真正想要与你的实际项目做什么。
To achieve the output you want, both threads will need to control their execution with a condition and signal each other before suspending themselves. However, this may or may not be what you actually want to do with your real project.
另外要注意:它其实并不重要,你创造了什么为了线程创建线程不会产生创建线程。因此,主线程可能会创建两个线程被调度无论是之前,线程调度器可以自由地安排执行其中的任何一个下一个。如果S-线程并在平台上先运行,这恰好是你的平台上实施的行为,是不是应该依靠。
Another note: it doesn't really matter what order you created the threads in. Creating a thread does not yield the creating thread. So the main thread will probably create both threads before either gets scheduled, and the thread scheduler is free to schedule either one of them for execution next. If the s-thread does run first on your platform, that just happens to be the implementation behavior on your platform and is not something that should be relied on.
这篇关于Pthread的条件信号 - 无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!