Thread killing problem

J

J.K. Baltzersen

To whomever it may concern:

I am using MS Visual C++ 6.0.

I have a process A which instantiates an object C.

At a later point the process A creates the thread B.

The thread B has access to the object C.

Because the user cancels the "process" which the thread B handles, the
thread B is stopped by the use of TerminateThread.

A bit later on I try to access member variables in the object B, the
purpose of this being replacing some files with backup versions of
these same files. These member variables are of type std::string.
Let's call these m, n, and o. When I access m, there seems to be no
problem. However, when I access n, the debugger hangs, apparently
infinitely.

I tried replacing std::string with char*, but that only resulted in
the problem showing up when I accessed m.

I want to be able to run TerminateThread on the thread B without my
object C being corrupted.

I would greatly appreciate any tips that would lead to my being able
to do so.

Thank you very much in advance for any help.

Best regards,
J.K. Baltzersen
 
Y

yanlinlin

To whomever it may concern:

I am using MS Visual C++ 6.0.

I have a process A which instantiates an object C.

At a later point the process A creates the thread B.

The thread B has access to the object C.

Because the user cancels the "process" which the thread B handles, the
thread B is stopped by the use of TerminateThread.

A bit later on I try to access member variables in the object B, the
purpose of this being replacing some files with backup versions of
these same files. These member variables are of type std::string.
Let's call these m, n, and o. When I access m, there seems to be no
problem. However, when I access n, the debugger hangs, apparently
infinitely.

I tried replacing std::string with char*, but that only resulted in
the problem showing up when I accessed m.

I want to be able to run TerminateThread on the thread B without my
object C being corrupted.

I would greatly appreciate any tips that would lead to my being able
to do so.

Thank you very much in advance for any help.

Best regards,
J.K. Baltzersen

Don't use TerminateThread. Use event or something else to notify the
thread to exit by itself instead.
 
J

J.K. Baltzersen

Don't use TerminateThread. Use event or something else to notify the
thread to exit by itself instead.- Hide quoted text -

- Show quoted text -

My thread is not event oriented. It runs from start to end unless some
exception is thrown. Could I tell the thread to throw an exception, by
sending a message to it, at an arbitrary point? I would rather avoid
introducing checkpoints where the thread decides whether to continue
or exit.

Thanks again.
 
Y

yanlinlin

My thread is not event oriented. It runs from start to end unless some
exception is thrown. Could I tell the thread to throw an exception, by
sending a message to it, at an arbitrary point? I would rather avoid
introducing checkpoints where the thread decides whether to continue
or exit.

Thanks again.

Sorry to misguide you. What I mean about the event is not the event
supported by OS, but just a notification.
Maybe you can do it like this:

volatile bool flag = false; // this is a global variable for notifying

DWORD WINAPI TheThreadProc(LPVOID) // this is the thread proc
{
// ...
while ( ! flag)
{
// ...
if (flag) break;
// ...
}
return 0;
}

void Foo()
{
HANDLE hThread = CreateThread(...);
// ...
flag = true; // Set the variable to let the thread exit by itself
WaitForSingleObject(hThread);
// ...
}

Since TerminateThread can not guarantee variables in thread be
destroied correctly, let the thread exit by itself is the right way.
 
J

J.K. Baltzersen

Sorry to misguide you. What I mean about the event is not the event
supported by OS, but just a notification.
Maybe you can do it like this:

volatile bool flag = false; // this is a global variable for notifying

DWORD WINAPI TheThreadProc(LPVOID) // this is the thread proc
{
   // ...
   while ( ! flag)
   {
      // ...
      if (flag) break;
      // ...
   }
   return 0;

}

void Foo()
{
   HANDLE hThread = CreateThread(...);
   // ...
   flag = true; // Set the variable to let the thread exit by itself
   WaitForSingleObject(hThread);
   // ...

}

Since TerminateThread can not guarantee variables in thread be
destroied correctly, let the thread exit by itself is the right way.- Hide quoted text -

- Show quoted text -

Thanks.

However, redesigning this application to check for an exit flag at
every second (or whatever we might choose) would be very costly. So I
was hoping there could be a simpler way, such as sending an exception
to the thread that is to exit. In that way we would be using the
existing exception handling system. The thread would exit upon
catching the exception.

Again, thanks.
 
J

J.K. Baltzersen

Thanks.

However, redesigning this application to check for an exit flag at
every second (or whatever we might choose) would be very costly. So I
was hoping there could be a simpler way, such as sending an exception
to the thread that is to exit. In that way we would be using the
existing exception handling system. The thread would exit upon
catching the exception.

Again, thanks.- Hide quoted text -

- Show quoted text -

I've tried a solution with SuspendThread as well. There seems to be
some of the same problems with that. I've also thought about putting
the thread to sleep for such a long time that it won't wake up before
the entire process has exited. However, I haven't found a way of
putting a thread to sleep from outside.
 
Y

yanlinlin

Thanks.

However, redesigning this application to check for an exit flag at
every second (or whatever we might choose) would be very costly. So I
was hoping there could be a simpler way, such as sending an exception
to the thread that is to exit. In that way we would be using the
existing exception handling system. The thread would exit upon
catching the exception.

Again, thanks.

Since exception is within a thread, I'm afraid you can not do like
that.
 
Y

yanlinlin

I've tried a solution with SuspendThread as well. There seems to be
some of the same problems with that. I've also thought about putting
the thread to sleep for such a long time that it won't wake up before
the entire process has exited. However, I haven't found a way of
putting a thread to sleep from outside.

Use ResumeThread to wake up the suspened thread. Or use CreateEvent
and WaitForSingleObject instead of sleep for long time.
 
J

J.K. Baltzersen

Use ResumeThread to wake up the suspened thread. Or use CreateEvent
and WaitForSingleObject instead of sleep for long time.- Hide quoted text -

- Show quoted text -

When I try to access the member variables of object C while thread B
is under suspension, I get the same problem -- seemingly at least --
as I get when I have terminated the thread with TerminateThread.
 
K

Kenneth Porter

Since exception is within a thread, I'm afraid you can not do like
that.

You might be able to, but it's going to be platform-specific. Under the
hood, an exception on a 386 is typically implemented using the machine's
native exception machinery, and that same machinery is also used for OS-
specific asynchronous exceptions like task termination and keyboard
interrupt. One's compiler may have support for intercepting these kinds of
exceptions. For example, Windows compilers typically support "structured
exception handling" (SEH) which can "catch" these kinds of exceptions.
 
T

Tomás Ó hÉilidhe

J.K. Baltzersen said:
At a later point the process A creates the thread B.

The thread B has access to the object C.


The C++ standard doesn't mention anything about threads, however there
are C++ communities built up dedicated to portable multi-threaded
programming.

To achieve portable multi-threaded programming, they produce a "cross-
platform library" which people can use in developing an application that
can run on a wide variety of system.

There's currently no newsgroup in place for discussing cross-platform
programming in C++, which is why I've proposed the creation of
comp.lang.c++.cross-platform. Voting should start in the next day or two
(there's a load of red tape crap at the minute).

I'd appreciate if you'd vote.
 
J

James Kanze

To whomever it may concern:
I am using MS Visual C++ 6.0.
I have a process A which instantiates an object C.
At a later point the process A creates the thread B.
The thread B has access to the object C.
Because the user cancels the "process" which the thread B
handles, the thread B is stopped by the use of
TerminateThread.

I'm not too sure about the exact semantics of TerminateThread,
but in general, thread cancellation (or termination) must be
synchronized in some way or another. Under Posix,
pthread_cancel is really only advisory, and will only
"terminate" the thread at specific "cancellation points. Which
of course, may mean never, if the thread never encounters a
cancellation point. Also, the C++ binding of pthread_cancel is
undefined, and different implementations do it differently, even
on the same platform. If TerminateThread is more than advisory,
or doesn't have some sort of additional synchronization
semantics, then you can't use it directly either.
A bit later on I try to access member variables in the object B, the
purpose of this being replacing some files with backup versions of
these same files. These member variables are of type std::string.
Let's call these m, n, and o. When I access m, there seems to be no
problem. However, when I access n, the debugger hangs, apparently
infinitely.

You mean the object C, I presume. If thread B was in the middle
of modifying object C when it was terminated, then it probably
left object C in an inconsistent state.

If your code is modifying object C, anywhere, all accesses to
object C must be synchronized via some sort of mutex. And a
thread may not be terminted while it holds that mutex.
I tried replacing std::string with char*, but that only
resulted in the problem showing up when I accessed m.
I want to be able to run TerminateThread on the thread B without my
object C being corrupted.

In general, functions like TerminateThread (supposing the
semantics I think it has) can't be made to work, and should only
be used as a last resort. What you need is 1) some sort of
shared variable which the thread polls from time to time, and 2)
some way of "unblocking" the thread so that it can poll.
 
J

James Kanze

[...]
The volatile isn't necessary. Or rather, it isn't sufficient,
and once you have a sufficient mechanism, it isn't necessary.
(Also, most compilers---VC++, g++ and Sun CC, at least---don't
implement any significant semantics for it anyway.)

You need to synchronize the access to flag if you want this to
work. My own solution is generally to implement a
"terminateRequested" function, which throws an exception if
flag is true, e.g.:

void
terminateRequested()
{
boost::mutex::scoped_lock lock( ourTerminationMutex ) ;
if ( ourTerminateRequestedFlag ) {
throw TerminateRequestException() ;
}
}

and

void
requestTermination()
{
boost::mutex::scoped_lock lock( ourTerminationMutex ) ;
ourTerminateRequestedFlag = true ;
}

(This more or less supposes some sort of thread object, of which
these functions are members.)

The problem is that all but the simplest classes have class
invariants that must be maintained, and that during non-const
functions, these class invariants are temporarily not
maintained: the rule is only that the invariants must hold on
entry and on exit of each function, but not during execution of
the member functions themselves. (If maintaining the invariants
involves modifying several separate state objects, there's no
possible way that they could hold all of the time during a
member function.)
I've tried a solution with SuspendThread as well. There seems to be
some of the same problems with that.

Again, I'm not 100% sure about the semantics of the function (I
generally work on Posix based systems, or more recently on
Linux), but unless the suspention is somehow differed if you are
in a critical section, the lock won't be released, and no other
thread will be able to access the objects protected by the lock.
I've also thought about putting the thread to sleep for such a
long time that it won't wake up before the entire process has
exited. However, I haven't found a way of putting a thread to
sleep from outside.

And putting it to sleep won't solve the problem either, because
the sleeping thread will still have the lock.
 
J

James Kanze

The C++ standard doesn't mention anything about threads,
however there are C++ communities built up dedicated to
portable multi-threaded programming.

The current working draft of the C++ standard does mention
threads, and threads will be part of the next version of the
standard. I believe that it is still hoped that this version
will become official before the end of 2009 (so that it will be
C++0x, and not C++1x), although the schedule is very, very
tight.

Long before the current draft, Boost offered a portable
interface to threads, and before that, there was Ace, and
doubtlessly many others.
To achieve portable multi-threaded programming, they produce a
"cross-platform library" which people can use in developing an
application that can run on a wide variety of system.
There's currently no newsgroup in place for discussing cross-platform
programming in C++,

There's this group. Why do you keep claiming that this group
doesn't exist?
 
T

Tomás Ó hÉilidhe

James Kanze said:
The current working draft of the C++ standard does mention
threads, and threads will be part of the next version of the
standard. I believe that it is still hoped that this version
will become official before the end of 2009 (so that it will be
C++0x, and not C++1x), although the schedule is very, very
tight.


I've been out of the C++ loop for about a year or two. Is there a summary
anywhere on the web of all the new features that will make it into the
next standard?

There's this group. Why do you keep claiming that this group
doesn't exist?


Because people get told to shut up if they ask how to interface with a
USB device in C++.
 
E

Erik Wikström

I've been out of the C++ loop for about a year or two. Is there a summary
anywhere on the web of all the new features that will make it into the
next standard?

http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2007/n2432.html
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2007/n2433.html
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/

Of course nothing is set in stone just yet, but I would suspect that
most of the listed features stuff will be in an not much more.
 
C

Chris Thomasson

J.K. Baltzersen said:
To whomever it may concern:

I am using MS Visual C++ 6.0.

I have a process A which instantiates an object C.
Okay.


At a later point the process A creates the thread B.

I see.

The thread B has access to the object C.
Alright.


Because the user cancels the "process" which the thread B handles, the
thread B is stopped by the use of TerminateThread.

Wait a minute here... What process does thread B handle? AFAICT, Process A
owns Thread B, not the other way around. Are you talking about an external
Process B which thread B communicates with or something?

How does the user cancel the process A, or B for that matter? Gracefully
from within your program's interface, or Ctrl-C or something more drastic?
TerminateThread is a nasty function to call. Its generally a sign of a clear
design flaw in your thread synchronization scheme.

[...]
 
J

J.K. Baltzersen

To whomever it may concern:
I am using MS Visual C++ 6.0.
I have a process A which instantiates an object C.
Okay.

At a later point the process A creates the thread B.

I see.
The thread B has access to the object C.
Alright.

Because the user cancels the "process" which the thread B handles, the
thread B is stopped by the use of TerminateThread.

Wait a minute here... What process does thread B handle? AFAICT, Process A
owns Thread B, not the other way around. Are you talking about an external
Process B which thread B communicates with or something?

How does the user cancel the process A, or B for that matter? Gracefully
from within your program's interface, or Ctrl-C or something more drastic?
TerminateThread is a nasty function to call. Its generally a sign of a clear
design flaw in your thread synchronization scheme.

[...]

My application is for checking in changes in a local database to a
central server. It has a main window. In this main window is Cancel
button. The check-in procedure is run in a thread. When the user
presses the Cancel button, I want the procedure (referred previously
to as "process") to terminate.

Before the check-in procedure starts, I have backed up my database
files. The object that is accessed by the main process and the thread
to be ended keeps track of information needed to roll back the
database, i.e., through file copy.

When the cancel operation for the check-in procedure has completed, I
need to access the object in order to perform the rollback. This is
what fails, as when accessing one of the member variables of the
object, the debugger hangs, i.e., it never completes the simple assign
operation that involves this particular member variable.

I've tried using SuspendThread instead of TerminateThread, but there
seems to be a similar problem with that approach. I get a hang, but
the problem is not necessarily with the same member variable.

All I need is for the thread to stop execution at any chosen time. It
can be temporarily, through suspend, sleep, or whatever. If I can
achieve this, I can have the execution stopped for enough time for the
rollback to complete, and when the rollback is complete, the whole
process is to end anyway.
 
I

Ioannis Gyftos

All I need is for the thread to stop execution at any chosen time. It
can be temporarily, through suspend, sleep, or whatever. If I can
achieve this, I can have the execution stopped for enough time for the
rollback to complete, and when the rollback is complete, the whole
process is to end anyway.

Which thread library are you using?

(and, just to add something to the conversation)

I had some sort of similar problem once in an ACE environment.
Suspending the thread didn't work for some reason (my platform didn't
support it?), so I dealt with it by using timed mutexes. One thread
would do some stuff, then use a timed mutex to sleep until it was
woken up by another thread or X seconds passed. I also got an
indication that time out occurred from ACE framework as well, so I
could handle that occasion differently. In any event, it would return
normally without terminate of any soft.

I am not sure if this applies to you (ie. that thread is aware it
should suspend itself or it has to be triggered externally etc), but
it's a thought.
 
J

James Kanze

[...]
Because people get told to shut up if they ask how to
interface with a USB device in C++.

And what makes you think that the people telling others to shut
up won't do it in the new group as well?

Note that if someone just asks how to interface with a USB
device, the correct answer here is that he'll probably need to
use a platform specific API, since there isn't a cross-platform
solution (that I know of). Someone who asks how to write a GUI,
however, will (or should) be sent to wxWidgets or something
similar. Someone who asks how to open a window in VC++ under
Windows, on the other hand, should be sent to
comp....mswindows... Because that isn't a cross-platform
question.

And I know that there are a few people who have nothing better
to do than to jump on people for posting to the wrong group,
even when it's not the case. But I don't think you can do much
about that, and I don't think a new group will help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top