LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2010, 07:30 AM   #1
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 154

Rep: Reputation: 15
how to close the thread externally


i created one thread in main(). I have while in thread function.

I want close the thread in main after some time. How can i close it.

Please help me.

Regards,
Nagendra R
 
Old 08-11-2010, 08:24 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Try pthread_cancel().

If that does not work, then try designing your s/w such that you can notify the thread via some other means (ie setting a semaphore or other data type).

P.S. A simple example:
Code:
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>

void* run(void* arg)
{
   while (1) {
      printf("The thread is running.\n");
      sleep(2);
   }

   return 0;
}

int main(void)
{
   pthread_t tid;

   pthread_create(&tid, 0, run, 0);

   sleep(10);

   pthread_cancel(tid);   // thread will stop executing here.

   sleep(4);

   return 0;
}

Last edited by dwhitney67; 08-11-2010 at 08:28 AM.
 
Old 08-11-2010, 08:41 AM   #3
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 154

Original Poster
Rep: Reputation: 15
I tried like this . But does not work.

Here after cancel the thread, i delete the object passing to the thread function. Then I am getting crash because it is used by thread function after cancel the function also.

Please help me to solve this problem in another way.

Regards,
Nagendra R
 
Old 08-11-2010, 08:48 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by nagendrar View Post
I tried like this . But does not work.

Here after cancel the thread, i delete the object passing to the thread function. Then I am getting crash because it is used by thread function after cancel the function also.

Please help me to solve this problem in another way.

Regards,
Nagendra R
Ok, wait for a moment while I put on my ESP-enhancing hat, so that I can visualize your code....
 
Old 08-11-2010, 08:15 PM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by nagendrar View Post
I tried like this . But does not work.
Of course it doesn't. You have opened a can containing more worms than one can possibly imagine. That is a cute way of saying there are many, many potential problems raised by asynchronous canceling of a thread. You're lucky that the program consistently fails. A much worse situation would be if the program ran most of the time, but failed only occasionally. This can easily happen with POSIX threads.

To understand why all this is true, you should run (not walk) to your nearest bookseller and buy David R. Butenhof's excellent book Programming with POSIX Threads, published by Addison Wesley. (The copy I have of a similar book published by O'Reilly looks complete, but is sufficiently incomplete as to be dangerous.)

When you have read this book, you may find it easier simply to rewrite your code so that the thread that is to be canceled somehow finds out about this desire, and terminates by itself, gracefully.

Hope this helps.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Close Thread cbl48 LQ Suggestions & Feedback 3 03-05-2010 06:57 AM
Please un-close my thread TwinReverb Slackware 2 12-30-2009 09:01 AM
How to close a Thread btexpress LQ Suggestions & Feedback 7 05-19-2004 03:58 PM
Can a thread starter close the thread? ppuru LQ Suggestions & Feedback 3 07-26-2003 06:51 PM
how to close the thread chinmay nautiya Linux - Software 4 07-01-2003 09:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration