Re: Thread creation

From: Roy Fine (rlfine_at_twt.obfuscate.net)
Date: 04/08/04

  • Next message: Sz Borbely: "Re: Retrofitting base class with pure virtual function"
    Date: Wed, 7 Apr 2004 21:55:52 -0400
    
    

    Moiz

    Your code looks good at first glance - in fact it is quite similar from the
    model that I use - except I would turn off the autodelete and forego the
    duplicate handle.

    I tend to wrap the whole mess in a separate class and keep all things
    related encapsulated therein. I have adopted/adapted my std model to the
    methods you presented, and all seems to work well.. I cobbled together a
    quick hack and it is appended here for your review.

    My guess now would be that the problem lies elsewhere - specifically, what
    are you doing in the worker thread code with the "this" pointer - thread
    safe stuff I would assume.

    On which line of code do you get the "crash", and exactly what is the
    crash - i.e. memory access exception, unhandled exception, etc... What does
    the call stack look like after the "crash"? Can you trace back to somewhere
    in your code?

    regards
    roy fine

    /* ***************************** */
    class MYTHREAD{
    private:
      MYTHREAD();
      int ival;
      CString strVal;
    public:
      CWinThread *pthread;
      HANDLE thrdhandle;
    public:
      ~MYTHREAD(){
        ::WaitForSingleObject(thrdhandle,INFINITE);
        ::CloseHandle(thrdhandle);
        }
    MYTHREAD(int p_ival,CString p_strVal){
      ival = p_ival;
      strVal = p_strVal;
      pthread = AfxBeginThread(MYTHREAD::Worker, this,NULL,
               THREAD_PRIORITY_NORMAL, CREATE_SUSPENDED,NULL);

    ::DuplicateHandle(GetCurrentProcess(),pthread->m_hThread,GetCurrentProcess()
    ,
                    &thrdhandle,0,FALSE,DUPLICATE_SAME_ACCESS);
      pthread->ResumeThread();
      }
    static unsigned int Worker(void *p){
      int someVal;
      CString someStr;
      MYTHREAD *baseClass = (MYTHREAD *)p;
      someVal = baseClass->ival*baseClass->ival;
      CString tstr;
      tstr.Format("%s. and the square of %d is
    %d",baseClass->strVal,baseClass->ival,someVal);
      baseClass->strVal = tstr;
      ::Sleep(10000);
      return 0;
      }
    };

    "Moiz Mehmood" <anonymous@discussions.microsoft.com> wrote in message
    news:04F995BC-F219-4CAA-9894-C35516D05E09@microsoft.com...
    >
    > Hello,
    > I am trying to create a thread using AfxBeginThread. I then wait for that
    thread to finish using WaitForsingleObject. My code is crashing
    consistently. Below is the code snippet which is causing the crash. Can
    anyone point out what I am doing wrong?
    >
    > This code is part of a message handler.
    >
    > Thanks is advance
    > Moiz
    >
    > CWinThread* pThread = AfxBeginThread(LocalThread, this,
    THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
    > HANDLE threadHandle;
    > ::DuplicateHandle(GetCurrentProcess(),
    > pThread->m_hThread,
    > GetCurrentProcess(),
    > &threadHandle,
    > 0,
    > FALSE,
    > DUPLICATE_SAME_ACCESS);
    > pThread->ResumeThread();
    > ::WaitForSingleObject(threadHandle, INFINITE);
    > ::CloseHandle(threadHandle);
    >


  • Next message: Sz Borbely: "Re: Retrofitting base class with pure virtual function"

    Relevant Pages

    • Re: Inheritance
      ... CString GetTeammascot(); ... int FindTeam; // Get the index for a particular team given their team name by looping through list ... class glider: public monoplane { ... Given this hierarchy, I can probably come up with actual planes that don't fit it. ...
      (microsoft.public.vc.mfc)
    • Re: Newbie: out of memory issues
      ... Heres the problem code:: Lots of array handling with objects.. ... You should use CString. ... char out2; ... BOOL swe_date_conversion(int year, int month, int day, int hour, int cal, double & ...
      (microsoft.public.vc.mfc)
    • Re: How To Refresh tree
      ... > pane tree control to Right pane list control which contain Tree Item, ... > CString strItemQry; ... > int nItemPriKey = GetItemPrimaryKey; ... > // check for any sub chield under sub chield ...
      (microsoft.public.vc.mfc)
    • Re: CStringArray
      ... void InsertName(int modNum, int location, const CString & name) ... ATLASSERT(ValidIndexes(modNum, location)); ... CString GetName(int modNum, int location) const ...
      (microsoft.public.vc.mfc)
    • Re: [RFC] Linux Kernel Dump Test Module
      ... Please find below a patch for a simple module to test Linux Kernel Dump ... This module uses jprobes to install/activate pre-defined crash ... Dump Test Tool by Fernando. ... +struct block_device *bdev, unsigned int cmd, ...
      (Linux-Kernel)