Click here to Skip to main content
16,011,685 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: multiple adapter loopback test using CSockets Pin
Tim Deveaux15-Mar-02 11:42
Tim Deveaux15-Mar-02 11:42 
GeneralRe: multiple adapter loopback test using CSockets Pin
gus19-Mar-02 4:15
gus19-Mar-02 4:15 
GeneralList control refresh issue Pin
Andrew Stampor15-Mar-02 8:48
Andrew Stampor15-Mar-02 8:48 
GeneralRe: List control refresh issue Pin
Shog915-Mar-02 8:59
sitebuilderShog915-Mar-02 8:59 
GeneralThread questions........... Pin
15-Mar-02 8:34
suss15-Mar-02 8:34 
GeneralRe: Thread questions........... Pin
Andres Manggini15-Mar-02 9:47
Andres Manggini15-Mar-02 9:47 
GeneralRe: Thread questions........... Pin
Nemanja Trifunovic15-Mar-02 10:19
Nemanja Trifunovic15-Mar-02 10:19 
GeneralRe: Thread questions........... Pin
Christopher Duncan15-Mar-02 11:36
Christopher Duncan15-Mar-02 11:36 
Hi.

ChiYung wrote:
_beginthread(transaction1, 0, (void *) obj)

Make sure that obj is a pointer to a dynamically allocated object, or in some other way a pointer to data that won't magically disappear like a stack variable will.

ChiYung wrote:
2. I know....end_thread() is to end the thread....but can I suspend awhile and then resume a thread??? What are their function names?

SuspendThread() and ResumeThread() handle this. The standard, safe way for your thread to exit is when you exit your thread proc. Calling end_thread() or TerminateThread() is , as mentioned below, a Bad Thing.

ChiYung wrote:
If YES, how can i choose which thread to end/suspend/resume???

Take a look at using AfxBeginThread() instead. It returns a CWinThread* and that class contains a handle to the thread. That comes in handy when you want to make sure, for instance, that your thread has terminated before you do something else - you can call WaitForSingleObject(pThread->m_hThread, cbAlertable) to wait until the thread finishes - the handle will be signalled when it is.

Additionally, if you want to start a thread suspended, do some processing, and then resume the thread you can pass the suspended flag in to AfxBeginThread(), and then call ResumeThread() when you're ready to wake it up again.

One other thing that you reeeeeeeaally want to watch out for is data synchronization between threads. Check into the CMutex, CSemaphore & CCriticalSection classes to lock a data variable while you're using it and then unlock it when done. This prevents other threads from altering it underneath you, another Bad Thing.

The other bane of multithreaded programming is the dreaded race condition, where two threads are spinning along, and it's just sheer chance who does which operation first. That's a real pain in the tookus to debug. Using the synchronization objects and also using events (CreateEvent()) and waiting on them via WaitForSingleObject() can help there.

Of course, once you start working with blocking on events, etc. you run the risk of a deadlock, where 2 locks happen, but each is waiting for the other to let go, resulting in two threads that stand still until dinner at the Restaraunt at the End of the Universe (Milliway's, of course).

Ain't multithreaded programming fun? Actually, the best advice I have about threading is Obe Wan's line from the first Star Wars movie -

"Run, Luke!"


Chistopher Duncan
Author - The Career Programmer: Guerilla Tactics for an Imperfect World (Apress)
GeneralRe: Thread questions........... Pin
Tim Smith15-Mar-02 11:43
Tim Smith15-Mar-02 11:43 
GeneralRe: Thread questions........... Pin
Christopher Duncan15-Mar-02 15:23
Christopher Duncan15-Mar-02 15:23 
GeneralTHANK YOU ALL!!!!!!!!!!!!!!!!!!!!!!!!!!!! Pin
15-Mar-02 21:24
suss15-Mar-02 21:24 
GeneralDCOM with Visual C++ Pin
15-Mar-02 8:23
suss15-Mar-02 8:23 
GeneralRe: DCOM with Visual C++ Pin
Anand Amirineni15-Mar-02 14:54
Anand Amirineni15-Mar-02 14:54 
QuestionHow to stop this memory leak? Pin
Mark Tutt15-Mar-02 8:03
Mark Tutt15-Mar-02 8:03 
AnswerRe: How to stop this memory leak? Pin
Christian Graus15-Mar-02 10:27
protectorChristian Graus15-Mar-02 10:27 
GeneralRe: How to stop this memory leak? Pin
Mark Tutt15-Mar-02 10:32
Mark Tutt15-Mar-02 10:32 
GeneralStrings!!! Pin
15-Mar-02 7:49
suss15-Mar-02 7:49 
GeneralRe: Strings!!! Pin
Mike.NET15-Mar-02 8:00
Mike.NET15-Mar-02 8:00 
QuestionHow to make MFC apps use less RAM? Pin
Mike.NET15-Mar-02 7:44
Mike.NET15-Mar-02 7:44 
AnswerRe: How to make MFC apps use less RAM? Pin
Shog915-Mar-02 7:53
sitebuilderShog915-Mar-02 7:53 
GeneralVisual Studio .NET customization Pin
Todd Smith15-Mar-02 7:26
Todd Smith15-Mar-02 7:26 
GeneralCalling CListview in MDI application Pin
Jasmyn15-Mar-02 6:05
Jasmyn15-Mar-02 6:05 
Generalfurther details Pin
Jasmyn15-Mar-02 6:43
Jasmyn15-Mar-02 6:43 
Questiontoo simple question about modeless dialog? Pin
lucy15-Mar-02 5:11
lucy15-Mar-02 5:11 
AnswerRe: too simple question about modeless dialog? Pin
Tomasz Sowinski15-Mar-02 5:22
Tomasz Sowinski15-Mar-02 5:22 

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.