Click here to Skip to main content
15,910,234 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Help me Hook Copy/Paste Action. Pin
huynhnb16-Feb-06 20:43
huynhnb16-Feb-06 20:43 
GeneralRe: Help me Hook Copy/Paste Action. Pin
John R. Shaw16-Feb-06 21:44
John R. Shaw16-Feb-06 21:44 
Questionwindow design Pin
prabhathgk16-Feb-06 19:05
prabhathgk16-Feb-06 19:05 
AnswerRe: window design Pin
John R. Shaw16-Feb-06 19:31
John R. Shaw16-Feb-06 19:31 
QuestionQuestion about Threads Pin
Aqueel16-Feb-06 18:42
Aqueel16-Feb-06 18:42 
AnswerRe: Question about Threads Pin
Nick_Kisialiou16-Feb-06 19:12
Nick_Kisialiou16-Feb-06 19:12 
AnswerRe: Question about Threads Pin
QuickDeveloper16-Feb-06 19:16
QuickDeveloper16-Feb-06 19:16 
AnswerRe: Question about Threads Pin
Stephen Hewitt16-Feb-06 19:47
Stephen Hewitt16-Feb-06 19:47 
Your asking for trouble even thinking of calling TerminateThread. Here's an example program which shows the kind of thing that can go wrong:

------------------------------------------
#include <windows.h>

CRITICAL_SECTION g_cs;

void ShowMessageBox()
{
EnterCriticalSection(&g_cs);
MessageBox(NULL, "Hello", "ShowMessageBox", MB_OK);
LeaveCriticalSection(&g_cs);
}

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
ShowMessageBox();
return 0;
}

int main(int argc, char* argv[])
{
InitializeCriticalSection(&g_cs);

// Start thread.
DWORD ThreadID;
HANDLE hThread = CreateThread(NULL, 0, &ThreadProc, 0, 0, &ThreadID);

// Wait for one second then kill the thread.
// It still holds the CRITICAL_SECTION!!!
Sleep(1000);
TerminateThread(hThread, 0);

// We've killed the "ShowMessageBox" API, permanently, calling it now
// causes this thread to dead lock!
ShowMessageBox();

DeleteCriticalSection(&g_cs);

return 0;
}
------------------------------------------

In this program calling TerminateThread permanently breaks ShowMessageBox because we killed the thread when it still held a lock. This is the worst kind of bug as it's very hard to reproduce as it's sensitive to timing. If you're calling Win32 functions many of them will briefly hold locks, if you terminate a thread while it is holding one you're screwed.

Moral: Don't call TerminateThread. There are exceptions but very few.

The only real safe way to terminate a thread from another thread is to ask it nicely. This generally involves signaling an event and then waiting on the thread handle.

Steve

GeneralRe: Question about Threads Pin
Nick_Kisialiou16-Feb-06 19:54
Nick_Kisialiou16-Feb-06 19:54 
GeneralRe: Question about Threads Pin
Stephen Hewitt16-Feb-06 19:58
Stephen Hewitt16-Feb-06 19:58 
GeneralRe: Question about Threads Pin
Nick_Kisialiou16-Feb-06 20:14
Nick_Kisialiou16-Feb-06 20:14 
GeneralRe: Question about Threads Pin
Stephen Hewitt19-Feb-06 11:11
Stephen Hewitt19-Feb-06 11:11 
AnswerRe: Question about Threads Pin
BadKarma16-Feb-06 20:53
BadKarma16-Feb-06 20:53 
GeneralRe: Question about Threads [modified] Pin
BadKarma25-May-06 12:52
BadKarma25-May-06 12:52 
AnswerRe: Question about Threads Pin
Aqueel17-Feb-06 17:31
Aqueel17-Feb-06 17:31 
AnswerRe: Question about Threads Pin
ThatsAlok17-Feb-06 21:47
ThatsAlok17-Feb-06 21:47 
QuestionGetSafeHdc() allocates new memory or not Pin
anilksingh16-Feb-06 18:38
anilksingh16-Feb-06 18:38 
AnswerRe: GetSafeHdc() allocates new memory or not Pin
Ryan Binns16-Feb-06 19:45
Ryan Binns16-Feb-06 19:45 
AnswerRe: GetSafeHdc() allocates new memory or not Pin
John R. Shaw16-Feb-06 19:57
John R. Shaw16-Feb-06 19:57 
AnswerRe: GetSafeHdc() allocates new memory or not Pin
ThatsAlok17-Feb-06 3:42
ThatsAlok17-Feb-06 3:42 
QuestionQuestion about create simple image in c++/MFC Pin
Yanshof16-Feb-06 18:20
Yanshof16-Feb-06 18:20 
AnswerRe: Question about create simple image in c++/MFC Pin
Stephen Hewitt16-Feb-06 18:36
Stephen Hewitt16-Feb-06 18:36 
AnswerRe: Question about create simple image in c++/MFC Pin
John R. Shaw16-Feb-06 20:03
John R. Shaw16-Feb-06 20:03 
AnswerRe: Question about create simple image in c++/MFC Pin
ThatsAlok17-Feb-06 3:40
ThatsAlok17-Feb-06 3:40 
QuestionIssue with debug and release Pin
super16-Feb-06 16:03
professionalsuper16-Feb-06 16:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.