[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Use of Thread::Cancel
From: |
David Sugar |
Subject: |
Re: Use of Thread::Cancel |
Date: |
Mon, 17 Nov 2003 21:34:14 -0500 |
User-agent: |
KMail/1.5.3 |
"cancellation points" is a concept that exists in posix threading (pthreads).
The idea is that specific calls or functions will act as a cancellation
point, and these are defined typically as the sleep and yield call, a
semaphore call, and a wait on a conditional object.
Posix threads can be set to cancel either always, which may be bad, if, for
example, a cancel request happens in the middle of an action that allocates
or manipulates a system resource, at the stated cancellation points, or
never. This is why cancellation points are useful, you can use them to make
sure that at other times your thread will execute and operations will
complete without interruption.
Cancellation does not depend on a parent-child relationship because posix
threads has no such concept, rather any thread can already join any other
thread, unless a thread has been explicitly "detached", in which case no
thread can cancel (join) it.
Common C++ uses parent-child thread relationships as a convenience for thread
death notification. That is, a Common C++ thread object is aware of the
thread context of the thread it was created from. This could be useful for
applications which wish to maintain such relationships, but in no way effects
cancellation.
Cancellation can be simulated in w32 with the addition of an additional object
placed in artificially extended and alternate sleep, yield, and semaphore
functions, and the w32 port of Common C++ does this to simulate posix
behavior.
Here is an example of a loop where setCancel may be used:
for(;;)
{
failover();
setCancel(cancelImmediate);
time(&now);
if(now >= last + refresh)
{
if(nodes > 1)
elect();
... some other operations that may be interrupted
}
setCancel(cancelDeferred);
// file operations are cancellation points...
rtn = ::recvfrom(so, buffer, sizeof(node), 0,
(struct sockaddr *)&addr, &alen);
if(rtn < 1)
{
slog(Slog::levelWarning) << "network: input error..."
<< rtn << endl;
// so is sleep
Thread::sleep(5000);
continue;
The idea being you can use setCancel(cancelImmediate) to mark a section of
code that you do not care if it is interrupted, such as a tight for loop that
you dont want to waste a yield on but does not do any system operation.
On Monday 17 November 2003 08:15 pm, Matt Scifo wrote:
> Hello
>
> I am having some problems understanding exactly how to cancel a thread.
>
> I want to be able to issue a cancel request to a thread from another
> thread (not a parent) and have it cancel only if it has not reached a
> certain execution point. If that point has been reached, then the
> cancel shouldn't be performed until a set point.
>
> The docs talk about using cancelDeferred and setting a cancellation
> point "such as yield". I'm not quite sure how to set a specific
> cancellation point.
>
> Here is some exmaple code that shows what I am trying to do...
>
> while(!stop_running)
> {
> if (!_module_queue->empty())
> {
> void *target;
> // _module_queue is a class derived from Buffer.
> // This call blocks until something has been posted
> // to the queue.
> //
> // Do not cancel beyond this point until cancellation
> // point has been reached!
> _module_queue->wait(&target);
>
> while(start_important_operations_with_target)
> {
> // do not process a cancel() request until this
> // block finishes.
> }
>
> // set cancellation point here.
> // cancel() can now take affect.
> }
> }
>
>
> Can anyone offer any insight?
>
> I am using RH9, g++ 2.9.6, and commoncpp2 v1.0.13.
>
> Thanks
>
> Matt Scifo
>
>
>
> _______________________________________________
> Bug-commoncpp mailing list
> address@hidden
> http://mail.gnu.org/mailman/listinfo/bug-commoncpp
- Use of Thread::Cancel, Matt Scifo, 2003/11/17
- Re: Use of Thread::Cancel,
David Sugar <=
- Message not available