473,320 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Thread killing problem

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
18 2501
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
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
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
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
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
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
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
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
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
"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
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
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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: djw | last post by:
Hi, Folks- I have a question regarding the "proper" use of try: finally:... Consider some code like this: d = Device.open() try: d.someMethodThatCanRaiseError(...) if SomeCondition: raise...
10
by: Jacek Pop³awski | last post by:
Hello. I am going to write python script which will read python command from socket, run it and return some values back to socket. My problem is, that I need some timeout. I need to say for...
2
by: Sgt. Sausage | last post by:
New to multi-threading (less than 24 hours at it <grin>) Anyway, it's all making sense, and working fairly well thus far, but I'm having a minor issue I'm not sure how to get around. I've got...
22
by: nd02tsk | last post by:
Hello! I have a couple of final ( I hope, for your sake ) questions regarding PostgreSQL. I understand PostgreSQL uses processes rather than threads. I found this statement in the archives: ...
8
by: Rob | last post by:
Hello, I've got an issue where a process in a third party application has a dll which while exiting sort of freezes and runs away with processor cycles. i've written a block of code so that I...
51
by: Hans | last post by:
Hi all, Is there a way that the program that created and started a thread also stops it. (My usage is a time-out). E.g. thread = threading.Thread(target=Loop.testLoop) thread.start() ...
18
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for reasons I do not know, a Connect call will totally lock up the thread and never return. I am sure it has something to do...
6
by: Roger Heathcote | last post by:
sjdevnull@yahoo.com wrote: <snip> Fair point, but for sub processes that need to be in close contact with the original app, or very small functions that you'd like 100s or 1000s of it seems...
1
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, Is it possible "kill" the thread of Backgroundworker ? In my Dowork event, I have NOT While for do e.Cancel = true, only have a call to external COM. If I want cancel, calling...
7
by: dmitrey | last post by:
hi all, Is there a better way to kill threading.Thread (running) instance than this one http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496960 (it's all I have found via google). BTW,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.