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

  • Home
  • SEARCH
  • 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 806417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:14:22+00:00 2026-05-15T00:14:22+00:00

I wish to have two threads. The first thread1 occasionally calls the following pseudo

  • 0

I wish to have two threads. The first thread1 occasionally calls the following pseudo function:

void waitForThread2() {
  if (thread2 is not idle) {
    return;
  }
  notifyThread2IamReady(); // i.e. via 1st condition variable
  Wait for thread2 to finish exclusive access. // i.e. via 2nd condition variable.
}

The second thread2 is forever in the following pseudo loop:

for (;;) {
  Notify thread1 I am idle.
  Wait for thread1 to be ready. // i.e. via 1st condition variable.
  Notify thread1 I am exclusive.
  Do some work while thread1 is blocked.
  Notify thread1 I am busy. // i.e. via 2nd condition variable.
  Do some work in parallel with thread1.
}

What is the best way to write this such that both thread1 and thread2 are kept as busy as possible on a machine with multiple cores. I would like to avoid long delays between notification in one thread and detection by the other. I tried using pthread condition variables but found the delay between thread2 doing ‘notify thread1 I am busy’ and the loop in waitForThread2() on thear2IsExclusive() can be up to almost one second delay. I then tried using a volatile sig_atomic_t shared variable to control the same, but something is going wrong, so I must not be doing it correctly.

  • 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-05-15T00:14:23+00:00Added an answer on May 15, 2026 at 12:14 am

    I looks to me like you are trying to do a rendezvous (a term from Ada).

    The second thread is sitting, waiting for the first thread to call it, then it does some work immediately while the first thread waits, and some more work after the first thread is finished.

    The first thread is “calling” the second thread – with an immediate timeout if the second thread is unavailable to take the call.

    Ada supports this directly in the language, but assuming that porting to Ada isn’t an option…

    This could be implemented with three semaphores. Semaphore 1 indicates that thread 1 is ready to rendezvous. Semaphore 2 indicates thread 2 is ready to rendevous. Semaphore 3 indicates the rendezvous is complete.

    Thread 1: Defaults with Semaphore 1 acquired.

     if Semaphore 2.acquire(timeout = 0) is successful # Thread 2 is ready
         Semaphore 1.release() # Indicate I am ready
         Semaphore 3.acquire() # Wait until the rendevous is complete.
         Semaphore 3.release()
         Semaphore 1.acquire() # Indicate I am not ready
         Semaphore 2.release() # I am no longer using thread 2.
    
     Do concurrent work 
    

    Thread 2: Defaults with Semaphore 2 acquired.

     Loop forever
         Semaphore 3.acquire() # Indicate Rendevous is not complete.
         Semaphore_2.release() # Indicate I am ready
         Semaphore_1.acquire() # Wait for Thread 1 to be ready
         Joint processing
         Semaphore 1.release() # I am no longer using thread 1.
         Semaphore 3.release() # Rendevous is complete.
         Semaphore 2.acquire() # I am not ready
    
     Post-processing
    

    NOTE: Written from scratch, not tested. Looks a lot more complicated than I thought it would be when I started; have I missed something?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# project in VS2008 which I wish to build for two
Possible Duplicate: Locking main() thread Below you'll find my code, Main calls two threads,
I have two UIImage objects as follow: image1: image2: I wish to combine the
I have a class Polygon on which I wish to implement two iterators: one
I wish to have long and short forms of command line options invoked using
Here is the domain that I wish to have: public class Person { public
If I have a non-template (i.e. normal) class and wish to have a template
I have a query where I wish to retrieve the oldest X records. At
I have a 3d object that I wish to be able to rotate around
I have a list of objects I wish to sort based on a field

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.