bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Use of Thread::Cancel


From: Matt Scifo
Subject: Re: Use of Thread::Cancel
Date: Tue, 18 Nov 2003 09:46:42 -0800

Amol, 

Thanks for your suggestion.  Actually, I am already making use of an
object level "flag" variable to determine whether or not the thread
should continue running.  That flag is shown in my example code as
"stopRunning".  As you pointed out, that flag will only allow me to
check to see if the thread should internally terminate at specific
points where I check for it.  I agree that cluttering up my code with
these checks is not desirable.  

Signals are the next logical direction to achieve my goals.  I have
already implemented a main signal handler in my primary thread.  Being
this is C++ and my signal handlers must be non-class functions, I end up
using a global "flag" variable again.  My main thread checks for this
variable the same way I discussed above and then performs the necessary
class method calls to shutdown other threads.  

My application launches a dynamic number of threads usually in the
hundreds to thousands range and I'm not sure if creating a signal
handler for each individual thread is the best way to accomplish my
task.  If I only want to terminate say 100 threads, how would I send a
specific signal to a certain 100 threads?

Can you or anyone else offer any insight about this possibility? 

Thanks

Matt

On Tue, 2003-11-18 at 08:50, address@hidden wrote:
> Hi Matt,
> 
> It seems to me that your problem might be solved by simply 
> setting a "cancelFlag" in your thread class. 
> 
> The resulting code would be something like this:
> (inserted code marked by //**)
> 
> -------------------------code----------------------
> //**declare some object level flag here: short cancelThread=1;
> 
>  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.
> 
>          //**check for cancellation here
>          if (cancelThread>0) return;  //**
> 
>          //
>          // 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.
>          }
> 
>          //**check for cancellation here
>          if (cancelThread>0) return;  //** 
> 
>          // set cancellation point here.
>          // cancel() can now take affect.
>      }
>  }
> -------------------------------end code---------------------
> but this would allow you to check for cancellation only at 
> specific instants in your code. If you want a truly 
> asynchronous cancellation you would have to use signals which
> dont seem to supported in the GNU common CPP package. You would
> have to use C signal handlers (which are surprisingly easy to 
> write). If you need outline of code using signal handler, let
> me know.
> 
> Regards,
> ~ amol
> 
> 
> 
> 
> 
> 
> 
> 
> 
> Quoting Matt Scifo <address@hidden>:
> 
> > 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
> > 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]