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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:43:17+00:00 2026-05-23T16:43:17+00:00

I wrote a program in C++ under Linux. For thread I am using pthread.

  • 0

I wrote a program in C++ under Linux. For thread I am using pthread.
In the program I start the thread and this thread is running until I call the function, that should stop it. Here you can see my code.

bool isRunning = false;
pthread_t thread;

void startThread() {
    isRunning = true;
    int thread_return = pthread_create(&thread, NULL, runThread, NULL);
}

bool stopThread() {
    isRunning = false;
    // wait until the thread will be finished
    return true;
}

void* runThread(void* ptr) {
    while(isRunning) {
        // do smth.
    }
    pthread_exit(NULL);
}

When the thread was started, it will do smth. until the value of isRunning will be false and the main program itself will do other stuff. The problem is, when I call the function stopThread, this function just sets isRunning to false and not waiting until the thread will finish its task inside the while-loop.
My question is, how I can say to function stopThread, that it should wait for thread exit.

I tried to put pthread_join in stopThread function after I set isRunning to false. But sometimes it causes the problem, that the program waits infinitly. Because the thread was finished faster, than the program comes to wait for thread. Because the thread can be on different parts inside the while-loop. So I never know how much the thread need to exit.

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-23T16:43:17+00:00Added an answer on May 23, 2026 at 4:43 pm

    From what I understand in your question, you should be using the concept of thread joining.

    Like so,

    bool stopThread() {
        int returnCode;
        isRunning = false;
        void *status;
        rc = pthread_join(your_thread, &status);
    
        // wait until the thread will be finished
    
        if (rc) {
            printf("ERROR; return code from pthread_join() 
                    is %d\n", rc);
            exit(-1);
        }
        printf("Main: completed join with thread %ld having a status   
                of %ld\n",t,(long)status);
    
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing (under linux or windows+cygwin) a java program that needs to run a
My problem is this: I have a C/C++ app that runs under Linux, and
I wrote a simple server using socket API in C under linux which listens
This Java web-app I am using that other developers wrote is structured in the
I have a UDP file descriptor in a C++ program running under Linux. I
I wrote a program that is using the Perl POE Framework to realize a
I wrote a flesch reading program that uses another class. I was under the
I wrote a program in C with Ubuntu Linux and now I need to
I wrote a program using AutoIT to fetch information from a number of websites
I wrote a program that forks some processes with fork(). I want to kill

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.