Click here to Skip to main content
15,909,747 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralApplication hangs in release mode... Pin
Sreekanth Muralidharan11-Apr-05 18:40
Sreekanth Muralidharan11-Apr-05 18:40 
GeneralRe: Application hangs in release mode... Pin
ThatsAlok11-Apr-05 18:53
ThatsAlok11-Apr-05 18:53 
GeneralRe: Application hangs in release mode... Pin
Sreekanth Muralidharan11-Apr-05 19:52
Sreekanth Muralidharan11-Apr-05 19:52 
Questionis it possible ?....if so how? Pin
namaskaaram11-Apr-05 18:31
namaskaaram11-Apr-05 18:31 
AnswerRe: is it possible ?....if so how? Pin
Jack Puppy11-Apr-05 19:19
Jack Puppy11-Apr-05 19:19 
AnswerRe: is it possible ?....if so how? Pin
Christian Graus11-Apr-05 19:22
protectorChristian Graus11-Apr-05 19:22 
QuestionIs there any reason why we would want to use memcpy() instead of memmove()? Pin
Link260011-Apr-05 15:33
Link260011-Apr-05 15:33 
AnswerRe: Is there any reason why we would want to use memcpy() instead of memmove()? Pin
Jack Puppy11-Apr-05 17:17
Jack Puppy11-Apr-05 17:17 
GeneralRe: Is there any reason why we would want to use memcpy() instead of memmove()? Pin
Link260011-Apr-05 17:38
Link260011-Apr-05 17:38 
AnswerRe: Is there any reason why we would want to use memcpy() instead of memmove()? Pin
Ryan Binns11-Apr-05 17:57
Ryan Binns11-Apr-05 17:57 
Generalsave/load variables in a dialog Pin
Green Fuze11-Apr-05 15:33
Green Fuze11-Apr-05 15:33 
GeneralRe: save/load variables in a dialog Pin
Nick Z.11-Apr-05 18:31
Nick Z.11-Apr-05 18:31 
GeneralRe: save/load variables in a dialog Pin
Green Fuze12-Apr-05 0:35
Green Fuze12-Apr-05 0:35 
GeneralRe: save/load variables in a dialog Pin
ThatsAlok11-Apr-05 20:35
ThatsAlok11-Apr-05 20:35 
GeneralRe: save/load variables in a dialog Pin
Green Fuze12-Apr-05 2:55
Green Fuze12-Apr-05 2:55 
GeneralThread synchronization. Pin
Nick Z.11-Apr-05 13:50
Nick Z.11-Apr-05 13:50 
GeneralRe: Thread synchronization. Pin
Ryan Binns11-Apr-05 17:59
Ryan Binns11-Apr-05 17:59 
GeneralRe: Thread synchronization. Pin
Nick Z.11-Apr-05 18:09
Nick Z.11-Apr-05 18:09 
GeneralRe: Thread synchronization. Pin
markkuk12-Apr-05 0:23
markkuk12-Apr-05 0:23 
GeneralRe: Thread synchronization. Pin
Jack Puppy11-Apr-05 18:00
Jack Puppy11-Apr-05 18:00 
You should try using an event for this.

in the GUI thread:

<br />
// Create an event in non-signalled state that's a member variable of your dialog<br />
m_hCancelEvent = CreateEvent(NULL, TRUE, FALSE, NULL);<br />


then pass the event handle to the worker thread in the AfxBeginThread call.

The GUI can then signal the worker (say via a Stop button) using:

<br />
// Signal the event<br />
SetEvent(m_hCancelEvent);<br />


and the worker can test for that signal in the thread procedure using WaitForSingleObject (or WaitForMultipleObjects if you're tracking multiple events)

<br />
// Check for signal; Wait will return WAIT_OBJECT_0 when the event has been signalled<br />
if (::WaitForSingleObject(pYourThreadData->hCancelEvent, 0) == WAIT_OBJECT_0))<br />
  // bail out here<br />
else<br />
// do thread stuff<br />
<br />


You need to close the event when your dialog closes:

<br />
// CloseHandle will throw an exception if the handle is bad, so you should use a try block here<br />
CloseHandle(m_hCancelEvent);<br />


II)

If you need to read/write shared data between threads, it's best practice to use the appropriate sync object. This is a very simplified example.

<br />
// Global defines<br />
static CCriticalSection g_criticalSection;<br />
static int* g_pSharedValue;<br />


In Thread 1:

<br />
CSingleLock singleLock(&g_criticalSection);<br />
<br />
// Block the other thread<br />
singleLock.Lock();<br />
<br />
if (g_pSharedValue != NULL)<br />
{<br />
    // I give to you, 8 more units<br />
    *g_pSharedValue += 8;<br />
<br />
    // Then double the profits<br />
    *g_pSharedValue *= 2;<br />
}<br />
<br />
// OK, we're done, other thread can have at it now<br />
singleLock.Unlock();<br />
<br />


In Thread 2:

<br />
CSingleLock singleLock(&g_criticalSection);<br />
<br />
// Block the other thread; all statements until UnLock are atomic<br />
singleLock.Lock();<br />
<br />
// Hah! I delete your silly profits<br />
delete g_pSharedValue;<br />
g_pSharedValue = NULL;<br />
<br />
// OK, we're done, other thread can have at it now<br />
singleLock.Unlock();<br />
<br />


Without the criticial section here, Thread 2 may execute the statement "delete g_pSharedValue", and then pass control to Thread 1. g_pSharedValue is corrupt, and causes Thread 1 to barf.



Suspicious | :suss: Pssst. You see that little light on your monitor? That's actually a government installed spy camera. Smile and wave to big brother!
GeneralRe: Thread synchronization. Pin
Nick Z.11-Apr-05 18:12
Nick Z.11-Apr-05 18:12 
GeneralUNICODE COMPILATION ReplaceSel usage for char array. Pin
kristna11-Apr-05 11:19
kristna11-Apr-05 11:19 
GeneralRe: UNICODE COMPILATION ReplaceSel usage for char array. Pin
Graham Bradshaw11-Apr-05 12:05
Graham Bradshaw11-Apr-05 12:05 
GeneralRe: UNICODE COMPILATION ReplaceSel usage for char array. Pin
kristna11-Apr-05 22:55
kristna11-Apr-05 22:55 
GeneralMFC Worker Thread Problem Pin
CNewbie11-Apr-05 11:00
CNewbie11-Apr-05 11:00 

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.