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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:59:06+00:00 2026-06-07T03:59:06+00:00

In my problem, I create a pthread using pthread_create(), say myThread. They both share

  • 0

In my problem, I create a pthread using pthread_create(), say myThread.

They both share a common variable “done” for the main thread to tell the myThread stops reading from the socket.

in my pthread, I have a loop:

// done is a common variable
while (!done && socket->read(&buffer) == OK) {
    // do something....
}

Some times, I want to tell myThread to stop reading from socket, so I do:

done = true;
  void *test;
    pthread_join(myThread, &test);

Will this cause a race condition? i.e. will myThread not see my main thread update the value of ‘done’ before it blocks on the read call?

  • 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-07T03:59:09+00:00Added an answer on June 7, 2026 at 3:59 am

    Writing a variable in one thread and reading it in another needs synchronization (such as a mutex) to avoid race conditions.

    If this is a real socket and not a strange object, consider calling shutdown() to tear down the connection and wake up blocked threads while keeping the file descriptor valid, see Wake up thread blocked on accept() call. Once the read has failed, myThread locks the mutex and checks the done variable.

    Other methods include calling poll() on the socket and a pipe for delivering the shutdown message.

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

Sidebar

Related Questions

OK, here is how to re-create the problem had: Create a new project, using
I wrote a program in C++ under Linux. For thread I am using pthread.
I am using the pthreads library and when I create a thread I am
i'm working with a multi-threaded program (using pthreads) that currently create a background thread
Here is my problem: Create a constructor for a telephone number given a string
The problem: To create a recursive array function that walks through a three dimensional
I have a problem to create a preprocessor macro function, that concatenates two Strings
I have a problem for create a SQL job, in my local machine I
How can I solve this problem Cannot Create Forms. No MDI forms are currently
The problem is to create concentric and nested squares,starting with the largest square's side

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.