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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:15:02+00:00 2026-06-14T01:15:02+00:00

Possible Duplicate: kill thread in pthread Here after a source code containing a launch

  • 0

Possible Duplicate:
kill thread in pthread

Here after a source code containing a launch of thread and then after a while I want to kill it. How to do it ? Without make any change in the thread function

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

pthread_t test_thread;

void *thread_test_run (void *v) // I must not modify this function
{
    int i=1;
    while(1)
    {
       printf("into thread %d\r\n",i);
       i++; 
       sleep(1);
    }
    return NULL
}

int main()
{
    pthread_create(&test_thread, NULL, &thread_test_run, NULL);

    sleep (20);
    // Kill the thread here. How to do it?

   // other function are called here...

    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-14T01:15:05+00:00Added an answer on June 14, 2026 at 1:15 am

    You can use pthread_cancel() to kill a thread:

    int pthread_cancel(pthread_t thread);
    

    Note that the thread might not get a chance to do necessary cleanups, for example, release a lock, free memory and so on..So you should first use pthread_cleanup_push() to add cleanup functions that will be called when the thread is cancelled. From man pthread_cleanup_push(3):

    These functions manipulate the calling thread’s stack of
    thread-cancellation clean-up handlers. A clean-up handler is a
    function that is automatically executed when a thread is cancelled
    (or
    in various other circumstances described below); it might, for
    example, unlock a mutex so that it becomes available to other threads
    in the process.

    Regarding the question of whether a thread will be cancelled if blocking or not, it’s not guaranteed, note that the manual also mentions this:

    A thread’s cancellation type, determined by pthread_setcanceltype(3),
    may be either asynchronous or deferred (the default for new
    threads)
    . Asynchronous cancelability means that the thread
    can be canceled at any time (usually immediately, but the system does
    not guarantee this
    ). Deferred cancelability means that cancellation
    will be delayed until the thread next calls a function that is a
    cancellation point.

    So this means that the only guaranteed behaviour is the the thread will be cancelled at a certain point after the call to pthread_cancel().

    Note:
    If you cannot change the thread code to add the cleanup handlers, then your only other choice is to kill the thread with pthread_kill(), however this is a very bad way to do it for the aforementioned reasons.

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

Sidebar

Related Questions

Possible Duplicate: Kill Ajax requests using JavaScript using jQuery Here is the simple code
Possible Duplicate: Emacs: how to delete text without kill ring? I am sick of
Possible Duplicate: “The breakpoint will not currently be hit. The source code is different
Possible Duplicate: Is there any way to kill a Thread in Python? So this
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How to do the vector of sets in C++? I want to
Possible Duplicate: check whether internet connection is available with C# I just want to
Possible Duplicate: How do I kill a back ground running app in iphone? When
Possible Duplicate: PHP method chaining? So I can remember seeing in some code examples

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.