none
Multhread Error on 64bit machine RRS feed

  • Question

  • Hi,

    I have to read 3d files using an 3D rendering lib. When I read the files with main thread (only one thread) then more time is taking (nearly 40 sec) to read all the files.

    So I implemented multi thread program for reading. With this the reading time reduced up to 50%.

    Now my problem is, when I run this on Windows - 7 ultimate 64 bit machine then some times the application crashes while reading.

    My development environment is Windows - XP SP3 32 bit machine. I am using .NET 3.5 SP1 framework.

    I did this in C++. Here I am giving my code for the reference.

    //Globles

    struct ModelObj
    
    {
    
       int id;
    
    };
    
    ModelObj obj[NUM_OF_MODELS];
    
    
    
    Model model;//OBJ OF MODEL HANDLING CLASS
    
    
    
    HANDLE T[NUM_OF_MODELS];
    
    CWinThread *pThread[NUM_OF_MODELS];
    
    UINT ThreadProc1(LPVOID lpvoid)
    
    {
          ModelObj *temp = (ModelObj*)lpvoid;
          
    model.modelForMarker[temp->id] = osgDB::readNodeFile(model.fileNames[temp->id]);
    
    
          if(model.modelForMarker[temp->id])
    
          {
    
             model.mtForMarker[temp->id]->addChild(model.modelForMarker[temp->id].get());
    
    
          }
    
    
       return 0;
    
    }
    

    and in Main

    Code:
    
    .........
    
    
    
    
    
    for(int i=0;i<NUM_OF_MODELS;i++)
    
                            {
    
                                    obj[i].id=i;
    
    
    
                            }
    
      
    
    strcpy(model.fileNames[0],"osg_01.osg");
    
                            strcpy(model.fileNames[1],"osg_02.OSG");
    
                            strcpy(model.fileNames[2],"osg_03.OSG");
    
    
    
    DWORD ThreadId[NUM_OF_MODELS];
    
                            for(int i=0;i<NUM_OF_MODELS;i++)
    
                            {
    
                                  model.mtForMarker[i] = new osg::MatrixTransform;
    
                model.modelSwitch->addChild(model.mtForMarker[i].get());
    
    
    
                model.mtForMarker[i]->addChild(model.sound_root.get());
    
                 model.mtForMarker[i]->setUpdateCallback( model.soundCB.get() );
    
    
    
                //pThread[i]=new CWinThread;
    
                std::cout<<"Thread: "<<i<<std::endl;
    
                pThread[i] = AfxBeginThread (ThreadProc1, (LPVOID)&obj[i]);
    
    
    
                pThread[i]->m_bAutoDelete=FALSE;
    
    
    
                            }
    
    
    
                            for (int j = 0; j < NUM_OF_MODELS; j ++)
    
                            {
    
                                    T[j] = pThread[j]->m_hThread;
    
                            }
    
    
    
                            ::WaitForMultipleObjects(NUM_OF_MODELS, T, TRUE, INFINITE);
    
    
    
                            for (int j = 0; j < NUM_OF_MODELS; j ++)
    
                            {
    
                                    delete pThread[j];
    
                            }
    


    The 3D rendering lib is multhi threaded comparable.

    Is there any wrog with my code?

    Can you please help me why am I getting error in 64 bit machine.

    • Moved by Carl Daniel Thursday, September 20, 2012 7:07 PM Off topic in .NET BCL Forum (From:.NET Base Class Library)
    Thursday, September 20, 2012 6:59 AM

All replies

  • I don't know whether this is the right forum for your question because this forum is ".Net Base class library" related.

    Still, looking at your code, at least one thing looks strange for me.

    In your code, you are setting m_bAutoDelete after AfxBeginThread.

    pThread[i]->m_bAutoDelete=FALSE;

    By default, dwCreateFlags parameter assumes the value of 0. It means that the thread starts immediately.

    What if the processing in your worker thread is very much faster such that it does not get chance to set m_bAutoDelete to FALSE? It results in the deletion of thread immediately? Is it the reason for the crash? To be frank, I don't know the default value for m_bAutoDelete and that's why I am just speculating it instead of confirming it.

    One more information supporting my speculation is at the bottom note of the MSDN documentation for AfxBeginThread here. If you can't find it, the code is as follows:

    CWinThread* thread=AfxBeginThread(
      DL_Enroll_Base, 
      &ARGUMENT, 
      THREAD_PRIORITY, 
      STACK_SIZE, 
      CREATE_SUSPENDED);
    
    thread->m_bAutoDelete=FALSE;
    
    thread->ResumeThread();

    Please note that this thread is created in SUSPENDED state to allow m_bAutoDelete flag to set properly.

    As you know, the problems in Multithreading world can come from lot of dimensions. Race condition is one of them. Who knows your main thread is preempted by your own worker thread immediately after creation and it completes its task before getting preempted itself and gets deleted. Then you will end up setting the flag to an unavailable (deleted) thread which may crash.

    I know SUSPEND and RESUME are not good idioms. But, what if it is the only solution for your problem?

    Off-note: Please post your question in the right forum and you may get your solution from the correct experts and faster, probably at Parallel Computing in C++ and Native Code.

    Quick GOOGLEing resulted in similar question answered:

    Got any help from these answers?

    • Edited by Rajesh_Kannan Thursday, September 20, 2012 8:49 AM Similar question in StackOverflow
    Thursday, September 20, 2012 8:41 AM