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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:41:17+00:00 2026-05-24T05:41:17+00:00

I a have a C program which calls to threads. iret1 = pthread_create( &thread1,

  • 0

I a have a C program which calls to threads.

iret1 = pthread_create( &thread1, NULL, readdata, NULL);
iret2 = pthread_create( &thread2, NULL, timer_func, NULL);
pthread_join(thread2, NULL);

Thread 2 returns after performing some function, after which I want to stop the execution of thread 1. How should I do this?

  • 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-24T05:41:19+00:00Added an answer on May 24, 2026 at 5:41 am

    You can stop the thread using pthread_cancel:

    pthread_cancel(thread1);
    

    And in readdata:

    /* call this when you are not ready to cancel the thread */
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
    ...
    /* call this when you are ready to cancel the thread */
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    

    See the pthread_cancel man page for more information – there’s an example included.

    If you don’t want to use pthread_cancel, you can use a global flag that is set by the main thread and read by thread 1. Also, you can use any of the IPC methods, like establishing a pipe between the threads.

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

Sidebar

Related Questions

I have a program (NWShader) which hooks into a second program's OpenGL calls (NWN)
I have created a console test application which creates, an object & calls 2
Using POSIX threads & C++, I have an Insert operation which can only be
I have a python program that calls into a c++ library, which wishes to
I have a program which uses an io_service and several threads. It instantiates some
i have a program which is using several threads to execute some task. Each
I have a program which does a system call: latex somefile.latex This runs ok,
I have a C program which has a function call that is defined in
I have a GUI program, which would call a cmd in this GUI program.
I have a program which needs to behave slightly differently on Tiger than on

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.