Hello, I have a problem with interruption of threads. I have one function that I want to run in two different threads with different parameters. When one of the function's instances finishes it stops the other instance. I can't change the function's code, but I can write wrapper to it. My problem is in stopping the other thread. Using timed_join I find which thread is finished, but I can't find a good solution for stopping the second one. How should I use interrupt method correctly or suggest me for correct thread stopping function. BTW, I can't just leave the other thread to run, it's very high CPU consumer. Thank you all.
Vadim Ryvchin wrote:
Hello,
I have a problem with interruption of threads. I have one function that I want to run in two different threads with different parameters. When one of the function's instances finishes it stops the other instance. I can't change the function's code, but I can write wrapper to it. My problem is in stopping the other thread. Using timed_join I find which thread is finished, but I can't find a good solution for stopping the second one. How should I use interrupt method correctly or suggest me for correct thread stopping function. BTW, I can't just leave the other thread to run, it's very high CPU consumer.
The solution will depend on your function. You imply that *maybe* the function is determining when to stop based on a global shared variable, if this is the case then you may be out of luck unless you can change the function. No way to tell without knowing what is going on in your code. Please post some code. n
I can get the executed function as parameter so no way for me to use global or shared variable. I'm looking for solution using thread reference only. Thank you anyway, Vadim. -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Nigel Rantor Sent: Monday, December 07, 2009 17:49 To: boost-users@lists.boost.org Subject: Re: [Boost-users] Newbie question Vadim Ryvchin wrote:
Hello,
I have a problem with interruption of threads. I have one function that I want to run in two different threads with different parameters. When one of the function's instances finishes it stops the other instance. I can't change the function's code, but I can write wrapper to it. My problem is in stopping the other thread. Using timed_join I find which thread is finished, but I can't find a good solution for stopping the second one. How should I use interrupt method correctly or suggest me for correct thread stopping function. BTW, I can't just leave the other thread to run, it's very high CPU consumer.
The solution will depend on your function. You imply that *maybe* the function is determining when to stop based on a global shared variable, if this is the case then you may be out of luck unless you can change the function. No way to tell without knowing what is going on in your code. Please post some code. n _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I would suggest to introduce a Cancellation Token as parameter to both
functions. If one function becomes ready it sets the cancelation flag to
true, so that other function can check it and exit. In the most simple case
it can be a volatile boolean value passed by reference to both functions.
Hope that helps,
Ovanes
On Mon, Dec 7, 2009 at 1:44 PM, Vadim Ryvchin
Hello,
I have a problem with interruption of threads. I have one function that I want to run in two different threads with different parameters. When one of the function's instances finishes it stops the other instance. I can't change the function's code, but I can write wrapper to it. My problem is in stopping the other thread. Using timed_join I find which thread is finished, but I can't find a good solution for stopping the second one. How should I use interrupt method correctly or suggest me for correct thread stopping function. BTW, I can't just leave the other thread to run, it's very high CPU consumer.
Thank you all.
I can't change the function so it will check the token, that is my main
problem. I want to be able to stop the execution from outside the function.
Thank you anyway,
Vadim.
From: boost-users-bounces@lists.boost.org
[mailto:boost-users-bounces@lists.boost.org] On Behalf Of Ovanes Markarian
Sent: Monday, December 07, 2009 18:09
To: boost-users@lists.boost.org
Subject: Re: [Boost-users] Newbie question
I would suggest to introduce a Cancellation Token as parameter to both
functions. If one function becomes ready it sets the cancelation flag to
true, so that other function can check it and exit. In the most simple case
it can be a volatile boolean value passed by reference to both functions.
Hope that helps,
Ovanes
On Mon, Dec 7, 2009 at 1:44 PM, Vadim Ryvchin
Is that function exception-safe?
Regards,
Ovanes
On Mon, Dec 7, 2009 at 6:00 PM, Vadim Ryvchin
I can’t change the function so it will check the token, that is my main problem. I want to be able to stop the execution from outside the function.
Thank you anyway,
Vadim.
*From:* boost-users-bounces@lists.boost.org [mailto: boost-users-bounces@lists.boost.org] *On Behalf Of *Ovanes Markarian *Sent:* Monday, December 07, 2009 18:09 *To:* boost-users@lists.boost.org *Subject:* Re: [Boost-users] Newbie question
I would suggest to introduce a Cancellation Token as parameter to both functions. If one function becomes ready it sets the cancelation flag to true, so that other function can check it and exit. In the most simple case it can be a volatile boolean value passed by reference to both functions.
Hope that helps,
Ovanes
On Mon, Dec 7, 2009 at 1:44 PM, Vadim Ryvchin
wrote: Hello,
I have a problem with interruption of threads. I have one function that I want to run in two different threads with different parameters. When one of the function's instances finishes it stops the other instance. I can't change the function's code, but I can write wrapper to it. My problem is in stopping the other thread. Using timed_join I find which thread is finished, but I can't find a good solution for stopping the second one. How should I use interrupt method correctly or suggest me for correct thread stopping function. BTW, I can't just leave the other thread to run, it's very high CPU consumer.
Thank you all.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
There is no exception management in the function. Its code based on C and
migrated to C++. I can wrapper it with try catch.
Vadim.
From: boost-users-bounces@lists.boost.org
[mailto:boost-users-bounces@lists.boost.org] On Behalf Of Ovanes Markarian
Sent: Monday, December 07, 2009 19:34
To: boost-users@lists.boost.org
Subject: Re: [Boost-users] Newbie question
Is that function exception-safe?
Regards,
Ovanes
On Mon, Dec 7, 2009 at 6:00 PM, Vadim Ryvchin
Stopping the execution from outside is very fragile. The main problem is
that you don't know, what code is currently executed. A simple idea to
accomplish your task is to suspend the thread in which the function
executes. That will put the thread into the waiting state. Than you can
interrupt this waiting thread. But it is possible, that your code ends up in
a deadlock, because if the interrupted thread is not going to free all
resources used (like mutex locks or whatever) the code might deadlock.
Another point here is that you might likely produce race conditions. If you
suspend the thread which currently executes a critical region it might not
end up writing all data, so that if interrupt is issued the written objects
might have a corrupted state.
On the other hand if you have access to mutex variable (or whatever
synchronization primitive, which controls the critical region access within
the function to be cancelled) you can lock it and issue the thread
interruption. This will ensure that the function to be cancelled has exited
the critical region and is either waiting to acquire lock (can be cancelled)
or will enter the wait for lock and cancelled than. But that is not all ;)
Actually, at that point the function which issued the thread interrupt must
wait until the other function is cancelled (because interruption can only
happen if the thread is in the waiting state) and the interruption exception
(if using boost::thread_interrupt) is not propagated outside the cancelled
thread. Therefore, a wrapper function should be introduced which catches
the exception and notifies the cancellation requestor about successful
cancellation. Here another mutex+condition variable+flag tuple can be used.
Additionally, the canceller should ensure that the function did not exit
before (otherwise it will block indefinitely=>use the flag) and wait for
notification of condition variable with timeouts.
If you are interested I can summarize it in pseudo-code.
To get back to your post: exception !!management!! does not matter.
Exception SAFETY matters. You must ensure that this ported to C++ function
automatically free all acquired resources or unlocks other mutex etc., when
an exception is initiated.
But I would be really VERY careful with proposed solution. If someone
changes the function the entire approach might be broken. So don't interrupt
smth, what was not designed for interruption ;)
Regards,
Ovanes
On Mon, Dec 7, 2009 at 6:43 PM, Vadim Ryvchin
There is no exception management in the function. Its code based on C and migrated to C++. I can wrapper it with try catch.
Vadim.
*From:* boost-users-bounces@lists.boost.org [mailto: boost-users-bounces@lists.boost.org] *On Behalf Of *Ovanes Markarian *Sent:* Monday, December 07, 2009 19:34
*To:* boost-users@lists.boost.org *Subject:* Re: [Boost-users] Newbie question
Is that function exception-safe?
Regards,
Ovanes
On Mon, Dec 7, 2009 at 6:00 PM, Vadim Ryvchin
wrote: I can’t change the function so it will check the token, that is my main problem. I want to be able to stop the execution from outside the function.
Thank you anyway,
Vadim.
*From:* boost-users-bounces@lists.boost.org [mailto: boost-users-bounces@lists.boost.org] *On Behalf Of *Ovanes Markarian *Sent:* Monday, December 07, 2009 18:09 *To:* boost-users@lists.boost.org *Subject:* Re: [Boost-users] Newbie question
I would suggest to introduce a Cancellation Token as parameter to both functions. If one function becomes ready it sets the cancelation flag to true, so that other function can check it and exit. In the most simple case it can be a volatile boolean value passed by reference to both functions.
Hope that helps,
Ovanes
On Mon, Dec 7, 2009 at 1:44 PM, Vadim Ryvchin
wrote: Hello,
I have a problem with interruption of threads. I have one function that I want to run in two different threads with different parameters. When one of the function's instances finishes it stops the other instance. I can't change the function's code, but I can write wrapper to it. My problem is in stopping the other thread. Using timed_join I find which thread is finished, but I can't find a good solution for stopping the second one. How should I use interrupt method correctly or suggest me for correct thread stopping function. BTW, I can't just leave the other thread to run, it's very high CPU consumer.
Thank you all.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Vadim Ryvchin wrote:
Hello,
I have a problem with interruption of threads. I have one function that I want to run in two different threads with different parameters. When one of the function's instances finishes it stops the other instance. I can't change the function's code, but I can write wrapper to it. My problem is in stopping the other thread. Using timed_join I find which thread is finished, but I can't find a good solution for stopping the second one. How should I use interrupt method correctly or suggest me for correct thread stopping function. BTW, I can't just leave the other thread to run, it's very high CPU consumer.
Thank you all.
Hi, with no way to change the functions and no global variables to indicate that they should stop, I see two options: a) try to do some hacker's magic and manipulate the memory of the function in such a way that it stops but not crashes (the sheer thought of that approach makes me shiver in horror) b) start the functions in separate processes. Stopping the second process is much easier. Of course, then you still could have problems with open files and so on. HTH Roland
Thank you, process solution is really nice and that what I choose to do. Thanks, Vadim. -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Roland Bock Sent: Tuesday, December 08, 2009 10:15 To: boost-users@lists.boost.org Subject: Re: [Boost-users] Newbie question Vadim Ryvchin wrote:
Hello,
I have a problem with interruption of threads. I have one function that I want to run in two different threads with different parameters. When one of the function's instances finishes it stops the other instance. I can't change the function's code, but I can write wrapper to it. My problem is in stopping the other thread. Using timed_join I find which thread is finished, but I can't find a good solution for stopping the second one. How should I use interrupt method correctly or suggest me for correct thread stopping function. BTW, I can't just leave the other thread to run, it's very high CPU consumer.
Thank you all.
Hi, with no way to change the functions and no global variables to indicate that they should stop, I see two options: a) try to do some hacker's magic and manipulate the memory of the function in such a way that it stops but not crashes (the sheer thought of that approach makes me shiver in horror) b) start the functions in separate processes. Stopping the second process is much easier. Of course, then you still could have problems with open files and so on. HTH Roland _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
But don't forget, that you still might abandon some system wide mutexes
(e.g. named Windows mutexes) if these are not released properly.
Regards,
Ovanes
On Tue, Dec 8, 2009 at 5:25 PM, Vadim Ryvchin
Thank you, process solution is really nice and that what I choose to do.
Thanks, Vadim.
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Roland Bock Sent: Tuesday, December 08, 2009 10:15 To: boost-users@lists.boost.org Subject: Re: [Boost-users] Newbie question
Vadim Ryvchin wrote:
Hello,
I have a problem with interruption of threads. I have one function that I want to run in two different threads with different parameters. When one of the function's instances finishes it stops the other instance. I can't change the function's code, but I can write wrapper to it. My problem is in stopping the other thread. Using timed_join I find which thread is finished, but I can't find a good solution for stopping the second one. How should I use interrupt method correctly or suggest me for correct thread stopping function. BTW, I can't just leave the other thread to run, it's very high CPU consumer.
Thank you all.
Hi,
with no way to change the functions and no global variables to indicate that they should stop, I see two options:
a) try to do some hacker's magic and manipulate the memory of the function in such a way that it stops but not crashes (the sheer thought of that approach makes me shiver in horror)
b) start the functions in separate processes. Stopping the second process is much easier. Of course, then you still could have problems with open files and so on.
HTH
Roland _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
Nigel Rantor
-
Ovanes Markarian
-
Roland Bock
-
Vadim Ryvchin