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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:03:20+00:00 2026-06-04T23:03:20+00:00

How do you close a thread, when you done? like making sure nothing is

  • 0

How do you close a thread, when you done? like making sure nothing is open anymore or runing?

so far i know how to open it, but .. not how to close it right

int  iret1; 
pthread_t thread1;
char *message1;

void *multithreading1( void *ptr ) {
    while (1) {
        // Our function here
    }   
}

int main (int argc, char * const argv[]) {
    if( (iret1=pthread_create( &thread1, NULL, multithreading1, (void*) message1)) )
    {
        printf("Thread creation failed: %d\n", iret1);
    }
    return 0;
}
  • 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-04T23:03:22+00:00Added an answer on June 4, 2026 at 11:03 pm

    “How do you close a thread, when you done?”
    Either by just simple returning from that function or calling pthread_exit function.

    Note that calling return also causes the stack to be unwound and variables declared within start routine to be destroyed, thus it’s more preferable than pthread_exit function:

    An implicit call to pthread_exit() is made when a thread other than the thread in
    which main() was first invoked returns from the start routine that was used to
    create it. The function's return value shall serve as the thread's exit status.
    

    For more information also have a look at: return() versus pthread_exit() in pthread start functions

    “making sure nothing is open anymore or runing”
    Instead of making sure whether your thread is still running, you should wait for its termination by using pthread_join function.

    Here’s an example:

    void *routine(void *ptr) {
        int* arg = (int*) ptr; // in C, explicit type cast is redundant
        printf("changing %d to 7\n", *arg);
        *arg = 7;
        return ptr;
    }
    
    int main(int argc, char * const argv[]) {
        pthread_t thread1;
        int arg = 3;
        pthread_create(&thread1, NULL, routine, (void*) &arg);
    
        int* retval;
        pthread_join(thread1, (void**) &retval);
        printf("thread1 returned %d\n", *retval);
        return 0;
    }
    

    output

    changing 3 to 7
    thread1 returned 7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could I close tcp::socket in different thread from the sync-reading thread? It looks like:
Possible Duplicate: Cross-thread operation not valid I am trying to close the base of
This seems like a simple problem, but somehow I'm having issues with this code.
How can I start a thread which will run in the background but the
I know I'll feel like a tard when I figure this one out. I'm
I'd like to do the following in the right order: - Do a long
It works just fine when I run it in a thread, but I want
I know this has been asked before , but I don't think these solutions
I've made this producer-consumer sample, but I don't know why it freezes at the
From what I understand about twisted, nothing running in the reactor thread should block.

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.