+ -
当前位置:首页 → 问答吧 → 多线程在DEBUG下运行结果正常,但是在Realese下运行不正常。请高手指点,多谢。

多线程在DEBUG下运行结果正常,但是在Realese下运行不正常。请高手指点,多谢。

时间:2011-11-27

来源:互联网

有下面这样一个程序,在Debug下可以执行到Afxmessagebox( "执行成功 ");   ,但是在Release版本下运行不到。在while内死循环。
不知道为什么m_db-> f_fthread   =   TRUE;这句话不起作用。希望高手指点一二。

#include   "main.h "

typedef   struct{
        BOOL   f_fthread;
}T_DbInsert;

class   CTestDlg   :   public   CDialog
{
    public:
    {
          T_DbInsert   DbInsert1;
          T_DbInsert   DbInsert2;
          HANDLE   thread1;
          HANDLE   thread2;
          static   DWORD   WINAPI   InsertThread1(lpvoid   LpParamter);
    }
}

#include   "main.cpp "

DWORD   WINAPI   InsertThread1(lpvoid   LpParamter)
{
    T_DbInsert   *   m_db   =   (T_DbInsert*)LpParamter;
    m_db-> f_fthread   =   TRUE;
    return   0;
}

DWORD   WINAPI   InsertThread2(lpvoid   LpParamter)
{
    T_DbInsert   *   m_db   =   (T_DbInsert*)LpParamter;
    m_db-> f_fthread   =   TRUE;
    return   0;
}

void   main(void)
{
        bool   isend   =   false;
        thread1   =   CreteThread(NULL,0,InsertThread1,(lpvoid)&DbInsert1,0,NULL);
        thread1   =   CreteThread(NULL,0,InsertThread2,(lpvoid)&DbInsert2,0,NULL);
        while(!isend)
        {
              if((DbInsert1.f_fthread   ==   TRUE)   &&   (DbInsert2.f_fthread   ==   TRUE))
              {
                    isend   =   TRUE;
                    Afxmessagebox( "执行成功 ");
              }
        }
}

作者: yjueqtd   发布时间: 2011-11-27

你要先保证你的两个线程都执行了各自的赋值操作后 再进入while 才能执行成功

作者: heksn   发布时间: 2011-11-27

static DWORD WINAPI InsertThread1(lpvoid LpParamter); 
有两个线程函数
这里怎么只声明了一个?

作者: zpc38368330   发布时间: 2011-11-27

很明线,在release 版中,线程的执行速度没有main的执行速度快。专业地说是线程调度问题。

楼主既然已经在编写线程的程序了,想必对线程调度有了一定的了解。

你可以在main中这样:

C/C++ code

void   main(void)
{
        bool   isend   =   false;
        thread1   =   CreteThread(NULL,0,InsertThread1,(lpvoid)&DbInsert1,0,NULL);
        thread1   =   CreteThread(NULL,0,InsertThread2,(lpvoid)&DbInsert2,0,NULL);
        while(!isend)
        {
              if((DbInsert1.f_fthread   ==   TRUE)   &&   (DbInsert2.f_fthread   ==   TRUE))
              {
                    isend   =   TRUE;
                    Afxmessagebox( "执行成功 ");
              }
              Sleep(0); // 加这句的目的在于让程序有机会去执行去它线程。
        }
}



作者: hb19820102   发布时间: 2011-11-27

代码应该是没有问题
估计楼主是源代码更改弄出来的吧
.H里代码是MFC的,。CPP是C++的,声明的好像有点混乱

在DEBUG下调试,有些内存遗漏经常会发现不到,但如果在RELEASE下很容易就会暴露出来,VC比别的语言最让人头痛的就是内存遗漏,因为一旦发生这种情况找原因相比下有难度。
  C/C++ code
     while(!isend) 
        { 
              if((DbInsert1.f_fthread   ==   TRUE)   &&   (DbInsert2.f_fthread   ==   TRUE)) 
              { 
                    isend   =   TRUE; 
                    Afxmessagebox( "执行成功 "); 
              } 
//加这段看看有没有什么发现
     if(((DbInsert1.f_fthread   ==   TRUE)) Afxmessagebox( "1执行成功 "); 
if(((DbInsert2.f_fthread   ==   TRUE)) Afxmessagebox( "2执行成功 "); 

        } 
//两个线程里也不妨加个Afxmessagebox(。。。);反正release不好跟踪,加上后执行看看会不会发现什么可思考的地方

作者: zpc38368330   发布时间: 2011-11-27

引用 4 楼 zpc38368330 的回复:
//两个线程里也不妨加个Afxmessagebox(。。。);反正release不好跟踪,加上后执行看看会不会发现什么可思考的地方



MessageBox(NULL,"线程1","",MB_OK);

作者: zpc38368330   发布时间: 2011-11-27

引用 4 楼 zpc38368330 的回复:
代码应该是没有问题
估计楼主是源代码更改弄出来的吧
....

如果这只是LZ源程序的一段模拟代码。
有可能源程序哪里有内存遗漏了。
楼主可以查查所有动用指针的地方,或是否有什么new malloc 之类的没有释放内存

作者: zpc38368330   发布时间: 2011-11-27