473,781 Members | 2,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
18 2543
"J.K. Baltzersen" <jo******@pvv.o rgwrote 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++.c ross-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.o rgwrote:
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 "cancellati on 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 objektorientier ter Datenverarbeitu ng
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.o rgwrote:
On Dec 26, 3:01 pm, "J.K. Baltzersen" <jornb...@pvv.o rgwrote:
On Dec 26, 2:04 pm, yanlinlin <yanlinli...@gm ail.comwrote:
On Dec 26, 7:20 pm, "J.K. Baltzersen" <jornb...@pvv.o rgwrote:
On Dec 26, 12:00 pm, yanlinlin <yanlinli...@gm ail.comwrote:
On Dec 26, 5:42 pm, "J.K. Baltzersen" <jornb...@pvv.o rgwrote:
[...]
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(L PVOID) // 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
WaitForSingleOb ject(hThread);
// ...
}
You need to synchronize the access to flag if you want this to
work. My own solution is generally to implement a
"terminateReque sted" function, which throws an exception if
flag is true, e.g.:

void
terminateReques ted()
{
boost::mutex::s coped_lock lock( ourTerminationM utex ) ;
if ( ourTerminateReq uestedFlag ) {
throw TerminateReques tException() ;
}
}

and

void
requestTerminat ion()
{
boost::mutex::s coped_lock lock( ourTerminationM utex ) ;
ourTerminateReq uestedFlag = 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 objektorientier ter Datenverarbeitu ng
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.c omwrote:
"J.K. Baltzersen" <jornb...@pvv.o rgwrote 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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 6 '08 #14
James Kanze <ja*********@gm ail.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*********@gm ail.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.o rgwrote in message
news:77******** *************** ***********@a35 g2000prf.google groups.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.c omwrote:

[...]
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....mswindo ws... 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 objektorientier ter Datenverarbeitu ng
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
2522
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 Error # Error is subclass of Exception
10
9887
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 example: os.system("someapplication.exe") and kill it, if it waits longer than let's say 100 seconds
2
2499
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 a form that uses SqlDataAdapter. It fires off a thread to fill a DataSet. Not a big deal, this works. I've also got a requirement that the Thread that's going off to do the work -- if it takes too long, it has to
22
5073
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: "The developers agree that multiple processes provide more benefits (mostly in stability and robustness) than costs (more
8
12410
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 can drill down into the process, and get the offending Thread's ID (since the only ones that will have the issue have higher User processor Time). But, I can't figure out how to have my .Net app 'kill' or 'suspend' a Thread
51
54877
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() # This thread is expected to finish within a second
18
10245
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 with weirdness going on with the server I am talking to. Anyhow, this locked up state happens once in a while (maybe once per day) and I can't figure out how to deal with the locked up thread. If I issue a Thread.Abort() the exception never...
6
1908
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 like a kludge having to spawn whole new processes build in socket communications and kill via explicit OS calls. I can't see that approach scaling particularly well but I guess there's no choice. Does anyone think it likely that the threading...
1
10393
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 CancelAsync, not cancels the call to COM. How I can do it , please ? Any suggestions will be very appreciated.
7
6143
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, it should be noticed that lots of threading module methods have no docstrings (in my Python 2.5), for example _Thread__bootstrap, _Thread__stop.
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10143
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.