本帖最后由 job82824 于 2010-08-17 09:17:47 编辑

解决方案 »

  1.   

    CRITICAL_SECTION  是属于系统级可访问的,在进程,线程中都可以访问到,但要注意用完后释放,就这些问题你可以看看windows核心编程,讲得比交详细
      

  2.   

    谢谢。主要是现在程序有bug,必须要加上同步。所以想知道习惯性的定义方式--就是临界区的通常定义类型是?
      

  3.   

    自定义一个类,类成员就是临界区的变量,初始化时EnterCriticalSection,析构时LeaveCriticalSection
    在需要同步时,在{}里生成类的对象
      

  4.   

    如果你用MFC,就直接用CCriticalSection这个类,你定义成全局变量吧,便于其它线程访问
      

  5.   

    呵呵,这个方法挺好的,但是看起来可能会比较耗时。我现在把临界区定义成类成员变量,但是在子线程中访问时每次都得&(pWnd->m_mySync1)感觉好累赘?
      

  6.   

    结构化异常处理:http://blog.csdn.net/diamont/archive/2009/06/11/4259590.aspxCRITICAL_SECTION 变量要申明成全局的吧  不然其他线程怎么访问呢
      

  7.   

    Using Critical Section Objects
    The following example shows how a thread initializes, enters, and leaves a critical section. As with the mutex example (see Using Mutex Objects), this example uses structured exception-handling syntax to ensure that the thread calls the LeaveCriticalSection function to release its ownership of the critical section object. // Global variable
    CRITICAL_SECTION CriticalSection; void main()
    {
        ...    // Initialize the critical section one time only.
        InitializeCriticalSection(&CriticalSection);     ...    // Release resources used by the critical section object.
        DeleteCriticalSection(&CriticalSection)
    }DWORD WINAPI ThreadProc( LPVOID lpParameter )
    {
        ...    // Request ownership of the critical section.
        __try 
        {
            EnterCriticalSection(&CriticalSection);         // Access the shared resource.
        }
        __finally 
        {
            // Release ownership of the critical section.
            LeaveCriticalSection(&CriticalSection);
        }    ...}
    csdn上面的sample,就是try catch  
      

  8.   

    CRITICAL_SECTION
    配对使用。中间使用break,return时一定要注意