473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to Cancel a Thread

Hello

I need some help with canceling a thread.
Here is a simple code that creates and cancels a thread. It does create a
thread BUT it doesnt cancel it, am I doing this wrong?
Thank you!

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

void* work(void* arg){ while (1); }
int main()
{
int tmp=0;
pthread_t thread;
process=pthread_create(&thread,NULL,work,NULL);
pthread_cancel(thread); // this doesnt work

cin>>tmp>>tmp;
return 0;
}
Jul 19 '05 #1
2 6988
Steven wrote:
Hello

I need some help with canceling a thread.
Here is a simple code that creates and cancels a thread. It does create a
thread BUT it doesnt cancel it, am I doing this wrong?
Thank you!
Very sorry - this is off-topic in c.l.c++ - try comp.unix.programmer ...

readt the faq ...

http://www.parashift.com/c++-faq-lite/

However - just a hint - this idea of asynchronously stopping a thread is
really bad. In a well behaved system, a thread may be "notified" to
stop itself but it is up to the thread to terminate. Your work thread
sits in an infinite loop - you need to teach it to terminate.

Read the man-page on pthread_cancel. The first paragraph on my man page
discusses exactly this thing.


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

void* work(void* arg){ while (1); }
int main()
{
int tmp=0;
pthread_t thread;
process=pthread_create(&thread,NULL,work,NULL);
pthread_cancel(thread); // this doesnt work

cin>>tmp>>tmp;
return 0;
}


Jul 19 '05 #2

Steven wrote:

Hello

I need some help with canceling a thread.
Here is a simple code that creates and cancels a thread. It does create a
thread BUT it doesnt cancel it, am I doing this wrong?


You're doing it wrong. You need to have either a cancelation point or
async-cancel-safe region in your work() function. Note that there are
two types of cancelation points -- "shall occur" and "may occur". The
entire async-cancel-safe region behaves like "may occur" point (I mean
that thread cancelation "may occur" at any point inside the region if
it isn't disabled). And don't forget to make your thread routine extern
"C"... and to pray that thread cancelation works in a non-braindamaged
way under your C++ implementation (but that's unlikely, unfortunately).
Stay away from async cancel if don't understand async cancel safety.
Add pthread_testcancel() to you loop and it "will work".

regards,
alexander.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mike - EMAIL IGNORED | last post by:
Using RoseRT with RedHat 8.0, I have a thread that is waiting for a cin, and another thread that wants to cancel the cin without waiting for a response from the keyboard. I tried destroying the...
1
by: VM | last post by:
How can I cancel all running processes in my application when I click on the Cancel button? When the Run button's clicked, it creates an instance of a specified class and runs a method that reads...
1
by: Jeffrey Kingsley | last post by:
I would like to cancel a call to BeginInvoke of a delegate (i.e. kill the thread the call was made on). This would be done when a timeout occures for the waitone call to the WaitHandle object of...
6
by: Ken Varn | last post by:
I have an ASP.NET form that may take a very long time to process a particular request. If the user closes the browser window, the request will continue to process until it completes. This is a...
1
by: holysmokes99 | last post by:
I am trying to implement a cancel button on my application that ingests a bunch of data from a text file into a database. I have not worked too much with threads, so I am having some trouble. I had...
3
by: JJ | last post by:
Is there any way to allow the user to cancel a thread which carries out a single long task. My form app's thread submits data to a web form via a webclient instance. There is some considerable...
1
by: David Veeneman | last post by:
Does anyone knmow why an asynchronous worker method would crash if the e.Cancel method is set to true, but not otherwise? I'm working on a demo app with an asynchronous call, using the .NET 2.0...
2
by: Robinson | last post by:
I can start an Asynchronous operation against a data source with SQLCommand.BeginExecuteReader, allowing me to loop, checking for user cancellation before the operation has completed, but how then...
16
by: parez | last post by:
I start a BackGroundWorker to populate a grid. It is started off in the ui layer The thread follows( cannot think of a better word) the path UI->Layer1->Layer2->Communication Layer and it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.