By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,423 Members | 1,327 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,423 IT Pros & Developers. It's quick & easy.

Thread killing problem

P: n/a
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
Dec 26 '07 #1
Share this Question
Share on Google+
18 Replies


P: n/a
On Dec 26, 5:42 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
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.
Dec 26 '07 #2

P: n/a
On Dec 26, 12:00*pm, yanlinlin <yanlinli...@gmail.comwrote:
On Dec 26, 5:42 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:


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.- 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.
Dec 26 '07 #3

P: n/a
On Dec 26, 7:20 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
On Dec 26, 12:00 pm, yanlinlin <yanlinli...@gmail.comwrote:
On Dec 26, 5:42 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
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.- 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.
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.
Dec 26 '07 #4

P: n/a
On Dec 26, 2:04*pm, yanlinlin <yanlinli...@gmail.comwrote:
On Dec 26, 7:20 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:


On Dec 26, 12:00 pm, yanlinlin <yanlinli...@gmail.comwrote:
On Dec 26, 5:42 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
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.- 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.

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.- Hidequoted 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.
Dec 26 '07 #5

P: n/a
On Dec 26, 3:01*pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
On Dec 26, 2:04*pm, yanlinlin <yanlinli...@gmail.comwrote:


On Dec 26, 7:20 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
On Dec 26, 12:00 pm, yanlinlin <yanlinli...@gmail.comwrote:
On Dec 26, 5:42 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
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.- 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.
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.- 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.
Dec 26 '07 #6

P: n/a
On Dec 26, 10:01 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
On Dec 26, 2:04 pm, yanlinlin <yanlinli...@gmail.comwrote:
On Dec 26, 7:20 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
On Dec 26, 12:00 pm, yanlinlin <yanlinli...@gmail.comwrote:
On Dec 26, 5:42 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
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.- 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.
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.
Since exception is within a thread, I'm afraid you can not do like
that.
Dec 26 '07 #7

P: n/a
On Dec 26, 10:10 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
On Dec 26, 3:01 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
On Dec 26, 2:04 pm, yanlinlin <yanlinli...@gmail.comwrote:
On Dec 26, 7:20 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
On Dec 26, 12:00 pm, yanlinlin <yanlinli...@gmail.comwrote:
On Dec 26, 5:42 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
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.- 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.
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.- 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.
Use ResumeThread to wake up the suspened thread. Or use CreateEvent
and WaitForSingleObject instead of sleep for long time.
Dec 26 '07 #8

P: n/a
On Dec 26, 4:49*pm, yanlinlin <yanlinli...@gmail.comwrote:
On Dec 26, 10:10 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:


On Dec 26, 3:01 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
On Dec 26, 2:04 pm, yanlinlin <yanlinli...@gmail.comwrote:
On Dec 26, 7:20 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
On Dec 26, 12:00 pm, yanlinlin <yanlinli...@gmail.comwrote:
On Dec 26, 5:42 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
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 notifythe
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.
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.- 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.

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.
Dec 26 '07 #9

P: n/a
yanlinlin <ya*********@gmail.comwrote in news:f2315d85-ee13-425b-85e2-
fb**********@b40g2000prf.googlegroups.com:
>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.
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.
Dec 27 '07 #10

P: n/a
"J.K. Baltzersen" <jo******@pvv.orgwrote in comp.lang.c++:
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.

--
Tomás Ó hÉilidhe

Jan 5 '08 #11

P: n/a
On Dec 26 2007, 10:42 am, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 6 '08 #12

P: n/a
On Dec 26 2007, 3:10 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
On Dec 26, 3:01 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
On Dec 26, 2:04 pm, yanlinlin <yanlinli...@gmail.comwrote:
On Dec 26, 7:20 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
On Dec 26, 12:00 pm, yanlinlin <yanlinli...@gmail.comwrote:
On Dec 26, 5:42 pm, "J.K. Baltzersen" <jornb...@pvv.orgwrote:
[...]
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
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.)
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);
// ...
}
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.)
Since TerminateThread can not guarantee variables in
thread be destroied correctly, let the thread exit by
itself is the right way.
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.
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.)
In that way we would be using the
existing exception handling system. The thread would exit upon
catching the exception.
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 6 '08 #13

P: n/a
On Jan 5, 8:33 pm, "Tomás Ó hÉilidhe" <t...@lavabit.comwrote:
"J.K. Baltzersen" <jornb...@pvv.orgwrote in comp.lang.c++:
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.
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?

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 6 '08 #14

P: n/a
James Kanze <ja*********@gmail.comwrote in comp.lang.c++:
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 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?

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

--
Tomás Ó hÉilidhe
Jan 6 '08 #15

P: n/a
On 2008-01-06 19:26, "Tomï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï ¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ " wrote:
James Kanze <ja*********@gmail.comwrote in comp.lang.c++:
>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?
http://www.open-std.org/JTC1/SC22/WG...007/n2432.html
http://www.open-std.org/JTC1/SC22/WG...007/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.

--
Erik Wikström
Jan 6 '08 #16

P: n/a
"J.K. Baltzersen" <jo******@pvv.orgwrote in message
news:77**********************************@a35g2000 prf.googlegroups.com...
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.

[...]

Jan 7 '08 #17

P: n/a
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.
Jan 7 '08 #18

P: n/a
On Jan 6, 7:26 pm, "Tomás Ó hÉilidhe" <t...@lavabit.comwrote:

[...]
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.

--
James Kanze (GABI Software) mailto:ja*********@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
Jan 7 '08 #19

This discussion thread is closed

Replies have been disabled for this discussion.