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 560347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:20:00+00:00 2026-05-13T12:20:00+00:00

In linux, how can synchronize between 2 thread (using pthreads on linux)? I would

  • 0

In linux, how can synchronize between 2 thread (using pthreads on linux)?
I would like, under some conditions, a thread will block itself and then later on, it will be resume by another thread. In Java, there is wait(), notify() functions. I am looking for something the same on pthreads:

I have read this, but it only has mutex, which is kind of like Java’s synchronized keyword. That is not what I am looking for.
https://computing.llnl.gov/tutorials/pthreads/#Mutexes

Thank you.

  • 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-13T12:20:01+00:00Added an answer on May 13, 2026 at 12:20 pm

    You need a mutex, a condition variable and a helper variable.

    in thread 1:

    pthread_mutex_lock(&mtx);
    
    // We wait for helper to change (which is the true indication we are
    // ready) and use a condition variable so we can do this efficiently.
    while (helper == 0)
    {
        pthread_cond_wait(&cv, &mtx);
    }
    
    pthread_mutex_unlock(&mtx);
    

    in thread 2:

    pthread_mutex_lock(&mtx);
    
    helper = 1;
    pthread_cond_signal(&cv);
    
    pthread_mutex_unlock(&mtx);
    

    The reason you need a helper variable is because condition variables can suffer from spurious wakeup. It’s the combination of a helper variable and a condition variable that gives you exact semantics and efficient waiting.

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

Sidebar

Related Questions

In short: How can I use Hg to synchronize repositories between two computers using
Under Linux I can open a directory using opendir and then use readdir to
Under Linux I can issue a gcc -Wl,--defsym,main=main_x .. However in Mac OSX 10
I m running iperf between two machines (linux) and I can observe the mtu
I have 3 questions about thread and process communication. Can the Linux function msgget(),
Is there a function in the C Library under Linux which can set the
I would like to set the date time of some VM servers to be
Is there a command like cat in linux which can return a specified quantity
I'm using rsync under cygwin to synchronize my music and pictures folders across two
In Linux I can view or change the ephemeral port range using the /proc/sys/net/ipv4/ip_local_port_range

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.