Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8324807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:12:36+00:00 2026-06-09T00:12:36+00:00

I am coding a telemetry system in C++ and have been having some difficulty

  • 0

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.

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.

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:12:38+00:00Added an answer on June 9, 2026 at 12:12 am

    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.

    It sounds like your problem can be solved by having both threads alternately wait on the condition variable. Eg. in thread 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:

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´m coding a CSS menu for Wordpress and I have this stupid problem: I
Coding Follow/Unfollow system in CodeIgniter(PHP) & Twitter-Bootstrap. Am also having routes active for the
Iam coding a solution for web automation, I have been using the default web
Coding Language: C# Framework: .NET We have built a software, which among many other
When coding in either C or C++, where should I have the #include 's?
Coding standards in place mean I do not have an option of using ajax
While coding I added print statements into some files to keep track of what
When coding with databases on the web with say php and mysql you have
Coding in ASP.NET 4.0 / javascript/ jQuery/ WebServices The Scenario I have an analytics
When coding up Ajax calls in ASP.Net MVC we have a lot of options

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.