简体   繁体   English

什么时候使用pthread屏障而不是条件等待和广播更合适?

[英]When is it more appropriate to use a pthread barrier instead of a condition wait and broadcast?

I am coding a telemetry system in C++ and have been having some difficulty syncing certain threads with the standard pthread_cond_timedwait and pthread_cond_broadcast. 我正在使用C ++编写遥测系统,并且在使用标准pthread_cond_timedwait和pthread_cond_broadcast同步某些线程时遇到了一些困难。

The problem was that I needed some way for the function that was doing the broadcasting to know if another thread acted on the broadcast. 问题是我需要一些方法来进行广播的功能,以了解另一个线程是否作用于广播。

After some hearty searching I decided I might try using a barrier for the two threads instead. 经过一番热烈的搜索,我决定尝试使用两个线程的屏障代替。 However, I still wanted the timeout functionality of the pthread_cond_timedwait. 但是,我仍然想要pthread_cond_timedwait的超时功能。

Here is basically what I came up with: (However it feels excessive) 这基本上就是我想出的:(但感觉过多)

Listen Function: Checks for a period of milliseconds to see if an event is currently being triggered. 侦听功能:检查一段时间以查看当前是否正在触发事件。

bool listen(uint8_t eventID, int timeout)
{  
    int waitCount = 0;  
    while(waitCount <= timeout)
    {  
        globalEventID = eventID;
        if(getUpdateFlag(eventID) == true)
        {
            pthread_barrier_wait(&barEvent);
            return true;
        }
        threadSleep(); //blocks for 1 millisecond
        ++waitCount;
    }
    return false;
}

Trigger Function: Triggers an event for a period of milliseconds by setting an update flag for the triggering period 触发功能:通过设置触发周期的更新标志,触发事件一段时间

bool trigger(uint8_t eventID, int timeout)
    int waitCount = 0;  
    while(waitCount <= timeout)
    {  
        setUpdateFlag(eventID, true); //Sets the update flag to true
        if(globalEventID == eventID)
        {
            pthread_barrier_wait(&barEvent);
            return true;
        }
        threadSleep(); //blocks for 1 millisecond
        ++waitCount;
    }
    setUpdateFlag(eventID, false);
    return false;
}

My questions: Is another way to share information with the broadcaster, or are barriers really the only efficient way? 我的问题:与广播公司分享信息的另一种方式,还是障碍真的是唯一有效的方式? Also, is there another way of getting timeout functionality with barriers? 此外,是否有另一种获得带有障碍的超时功能的方法?

Based on your described problem: 根据您描述的问题:

Specifically, I am trying to let thread1 know that the message it is waiting for has been parsed and stored in a global list by thread2, and that thread2 can continue parsing and storing because thread1 will now copy that message from the list ensuring that thread2 can overwrite that message with a new version and not disrupt the operations of thread1. 具体来说,我试图让thread1知道它正在等待的消息已经被thread2解析并存储在全局列表中,并且thread2可以继续解析和存储,因为thread1现在将从列表中复制该消息,确保thread2可以用新版本覆盖该消息,而不是破坏thread1的操作。

It sounds like your problem can be solved by having both threads alternately wait on the condition variable. 听起来你的问题可以通过让两个线程交替等待条件变量来解决。 Eg. 例如。 in thread 1: 在主题1中:

pthread_mutex_lock(&mutex);
while (!message_present)
    pthread_cond_wait(&cond, &mutex);
copy_message();
message_present = 0;
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);

process_message();

and in thread 2: 在线程2中:

parse_message();

pthread_mutex_lock(&mutex);
while (message_present)
    pthread_cond_wait(&cond, &mutex);
store_message();
message_present = 1;
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);

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

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