简体   繁体   English

使用条件变量的简单多线程堆栈实现

[英]Simple Multi-threading Stack Implementation using Conditional Variables

In our CS course we used POSIX Thread Programming to implement a simple stack data structure. 在我们的CS课程中,我们使用POSIX线程编程来实现简单的堆栈数据结构。 We therefore made use of pthread_cond_wait and pthread_cond_signal : 因此,我们使用了pthread_cond_waitpthread_cond_signal

pthread_mutex_t write_mutex; 
pthread_mutex_t read_mutex; 

pthread_cond_t write_cond; 
pthread_cond_t read_cond;

int read()
{
    pthread_mutex_lock(&read_mutex); 
    while(is_empty())
    {
        pthred_cond_wait(&read_cond, &read_mutex);
    }
    // read value [...]
    pthread_cond_signal(&write_cond);
    pthread_mutex_unlock(&read_mutex);

    return read_value;

}

The write function is implemented similarly but locking the write_mutex and signaling the read_cond instead. 写函数的实现类似,但锁定write_mutex信号read_cond

Q: My problem with this implementation is: Doesn't this require a 1:1 ratio between readings a writings due to the signaling? 问:我的实现问题是:由于信号的缘故,这在阅读文章与写作之间是否不需要1:1的比例吗? This implementation does not allow to write multiple items without any reads in between since every write requires a signal triggered in the read function (vice versa multiple reads). 由于每次写入都需要在读取功能中触发一个信号(因此多次读取),因此此实现方法不允许写入多个项目而之间不进行任何读取。

Is my understanding correct or am I missing something? 我的理解正确吗?还是我缺少什么?

Q2 How long is a signal "valid" after calling pthread_cond_signal(...) ? Q2调用pthread_cond_signal(...)后信号“有效”多长时间?

Q : My problem with this implementation is: Doesn't this require a 1:1 ratio between readings a writings due to the signaling? :我的实现问题是:由于信号的缘故,这在阅读文章与写作之间是否不需要1:1的比例吗? This implementation does not allow to write multiple items without any reads in between since every write requires a signal triggered in the read function (vice versa multiple reads). 由于每次写入都需要在读取功能中触发一个信号(因此多次读取),因此此实现方法不允许写入多个项目而之间不进行任何读取。

If the write() function is truly analogous to the read() function presented, then yes and no. 如果write()函数确实类似于所示的read()函数,则为是和否。 I think you're suggesting that the stack could never have more than one element, but that in particular is not so. 我认为您是在建议堆栈永远不能只有一个以上的元素,但事实并非如此。 Note how a thread entering your read() function and finding the stack nonempty will bypass waiting on the condition variable altogether. 请注意,进入您的read()函数并查找非空堆栈的线程将如何完全绕过条件变量的等待。 A thread waits to read only if the stack is empty. 如果堆栈为空,则线程仅等待读取。 The analog on the other side would be that threads wait to write only if the stack is full to capacity. 另一方面,类似的情况是线程仅在堆栈已满时才等待写入。

Q2 How long is a signal "valid" after calling pthread_cond_signal(...)? Q2调用pthread_cond_signal(...)后信号“有效”多长时间?

No time at all. 根本没有时间。 Only threads already waiting on a condition variable can be unblocked when that CV is signaled. 发出CV信号时,只有已经等待条件变量的线程才能被解除阻塞。 There is not afterward any memory of a signal having been received, even if no thread was unblocked by it. 此后,即使没有线程被阻塞,也没有任何已接收信号的存储。

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

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