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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:47:46+00:00 2026-05-22T14:47:46+00:00

I am writing a multithreaded program and i have this question: Suppose that, while

  • 0

I am writing a multithreaded program and i have this question:
Suppose that, while executing in the main thread, i want to terminate all
child-threads. I can’t just send them a termination signal cause i want them
to free dynamically allocated memory first. Can i define a specific signal handler
function in each thread function that is executed, which in turn is going to call
a cleanup function that i will write to do so? If not how can i accomplish my goal??

Thanks,
Nikos

  • 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-22T14:47:47+00:00Added an answer on May 22, 2026 at 2:47 pm

    Look at the man page for pthread_cancel:

    When a cancellation requested is acted on, the following steps occur for
    thread (in this order):

       1. Cancellation clean-up handlers are popped (in the reverse of the order in
          which they were pushed) and called.  (See pthread_cleanup_push(3).)
    
       2. Thread-specific data destructors are called, in an unspecified order.  (See
          pthread_key_create(3).)
    
       3. The thread is terminated.  (See pthread_exit(3).)
    

    So you can use pthread_cancel from your main, provided you have registered you cleanup handlers correctly using the above functions.

    (Do read that man page completely though, it has a lot of important information.)

    Edit: (from comments) If you plan on using PTHREAD_CANCEL_DEFERRED and need to insert a cancellation point somewhere in your code, then use pthread_testcancel. This function checks if a cancellation was requested. If that is the case, the cancellation is serviced (i.e. that call never returns). Otherwise it has no effect.

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

Sidebar

Related Questions

I am writing a multithreaded program in java. I have written something like this
I'm writing a multithreaded distributed networking algorithm. I've one thread that listens to new
Let's say I have a multithreaded C++ program that handles requests in the form
All, I am writing a multithread program. The class which extends Thread has a
I am writing a multithreaded program where I want to handle a possible Ctrl-C
If I have a data structure in a multithreaded application that I want to
I am writing a multithreaded C program and I have an error. I have
In a multithreaded C++ program, I have the equivalent of this running in one
I am on MacOSX. I am writing a multi threaded program. One thread does
I am writing a multithreaded client that uses an IO Completion Port. I create

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.