|
Hi All,
I Create some thread by using the function _beginthreadex.
I hold some handle to this thread
(ThreadHandle = (HANDLE) _beginthreadex(...))
And now i want to kill the running thread and i don't know how to do it.
thanks for any help...
|
|
|
|
|
Let the thread procedure function exit by itself.
Maxwell Chen
|
|
|
|
|
There's a few ways to do it.
The nice way:
Before starting the thread, create an event object (CreateEvent). Share this event handle with your thread, check it periodically -- and when it becomes signalled, have the thread exit itself. It's always best for the thread to exit gracefully.
HANDLE hEvent = CreateEvent();
_beginThreadEx( NULL, 0, YourThreadFunc, hEvent, 0, &ThreadID );
unsigned int __stdcall YourThreadFunc( LPVOID pData )
{
HANDLE hEvent = (HANDLE)pData;
while ( WaitForSingleObject(hEvent,0) != WAIT_OBJECT_0 )
{
DoSomething();
}
return 0;
}
SetEvent(hEvent);
The nasty way:
TerminateThread( threadHandle );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<a href="http://www.soonr.com">SoonR Inc -- PC Power delivered to your phone</a>
|
|
|
|
|
I cant in my code check for event ... but using TerminateThread look better option for now.
but I dont understand why dont use the TerminateThread ?
I rad about TerminateThread in MSDN - and they wrote "TerminateThread is a dangerous function that should only be used in the most extreme cases" - but i dont see any problem.
Can you explain ?
Thanks.
modified on Tuesday, March 11, 2008 3:31 AM
|
|
|
|
|
Because it is not a natural way to exit your thread. First, if you allocated some resources in your thread, there's no way you will be able to clean them if you are using TerminateThread.
Second, you have no control about what you are doing: you will terminate the thread whatever it was processing. It means you can terminate it in the middle of something important that will be left not terminated (e.g. if you communicate with an external device, you started to send some data to it and then brutally stop it, leaving the device in an unknown state).
It is far better to handle that yourself, using a flag for example: you check regularly if this flag is false, if that's the case, you exit nicely the thread function (the general idea is having a loop that checks for this flag).
|
|
|
|
|
Ok, now i see the problem
Thanks.
|
|
|
|
|
If this is your first jaunt into threading, you'll run into some problems. There's a whole bunch of things that you need to learn about in order to make threading work right.
But once you figure them out -- you'll LOVE threading!
The most important lesson is "Don't take shortcuts" -- a certain level of discipline is important when constructing a program that employs threads.
I think I've forgotten how to write an unthreaded program. What did we do before threading?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<a href="http://www.soonr.com">SoonR Inc -- PC Power delivered to your phone</a>
|
|
|
|
|
Hi,
Can anybody please explain, How virtual destructor works under the hood?
What happens exactly with virtual table?
Thanks in advance.
|
|
|
|
|
I think:
The destructor goes into the vtable.
When the destructor is called -- the top-most constructor is called first, executed, then in turn, the each of the parent destructors are called. The order is the opposite of the order that the constructors are executed.
If you didn't do a virtual destructor, the parent destructors are hidden. I, personally, always use virtual destructors.
Just off the top of my head.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<a href="http://www.soonr.com">SoonR Inc -- PC Power delivered to your phone</a>
|
|
|
|
|
Peter Weyzen wrote: When the destructor is called -- the top-most constructor is called first
typo ?
|
|
|
|
|
sure seems like one!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<a href="http://www.soonr.com">SoonR Inc -- PC Power delivered to your phone</a>
|
|
|
|
|
I assume you mean "When the (virtual)destructor is called -- the top-most constructor destructor is called first". If so, why don't you edit.
|
|
|
|
|
then your reply wouldn't make any sense.... and we don't want that!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<a href="http://www.soonr.com">SoonR Inc -- PC Power delivered to your phone</a>
|
|
|
|
|
|
I have the following error
Error 1 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup MSVCRTD.lib
I also have the WinMain defined and the subsystem set to Windows
and even the callign convention is __stdcall
Please help me urgently.. I am in need
Thanx in advance
|
|
|
|
|
How does your WinMain prototype look like?
Maxwell Chen
|
|
|
|
|
Project properties ->
Linker ->
Advanced ->
Entry Point->
wWinMainCRTStartup
wWinMainCRTStartup ...if App is UNICODE
WinMainCRTStartup ... if App is not UNICODE
|
|
|
|
|
Iam having a MFC application, in which we have a worker theread.
Some operation is happens in worker thread.And we call this application for 10 times it works well.But when we call this application for 25th time it crashes at 25th time.
Let me know what will be the reasons behind this.
According to me one of the reasons is data type matching.But intially we resolved this problem for 10th time then it works well till. 24th time and at 25th time it crashes.
According to me there will be some other reaons behind this crashing.
Let me reasons how to rsolve this type of crashing.
lavatema
|
|
|
|
|
We're not psychics. You haven’t provided enough information. What is the type of crash? Can you provide the call stack of the crash? Can you post the relevant code?
Steve
|
|
|
|
|
What does the debugger say?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
<a href="http://www.soonr.com">SoonR Inc -- PC Power delivered to your phone</a>
|
|
|
|
|
just try pinpoint the position where it is crashing, also just ?, are these 10 threads running syncronously, if yes, have you applied good sync logic!, if they sharing same data!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You/codeProject$$>
|
|
|
|
|
Besides, is there any buffer overrun?
Maxwell Chen
|
|
|
|
|
Sorry, the CPMRU (Code Project Mind Reader Unit) is out of order.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Its easy. I got it. All what u have to do is to put break point on the
thread function(F9 For all the lines) debug and wait for the application to crash.
Thats it.
|
|
|
|
|
can any one help me out to get the code for converting roman numerals to their equivalent decimal numbers.
|
|
|
|