临界区是什么?能举个例子说明吗?

解决方案 »

  1.   

    CCriticalSection cs;
    .................
    thread1()
    {............
    cs.Lock();
    variable ++;
    cs.Unlock();
    }thread2()
    {............
    cs.Lock();
    variable ++;
    cs.Unlock();
    }如上所示,临界区的作用就是防止多个线程同时访问同一个变量从而引起某些错误。
    thread1 Lock以后,thread2在Lock的时候会一直等待,直到thread1 Unlock.
    当然你可以在Lock与Unlock之间进行更复杂的操作。
      

  2.   


    临界区,可以解决在实例(和线程)之间共享数据(或唯一资源)所引起的问题,多线程的同步中用到CRITICAL_SECTION csRecvRead  = {0};
    InitializeCriticalSection(&csRecvRead);// 临界区初始化
    EnterCriticalSection(&csRecvRead);// 使用临界区变量
    pRightBuffer = pRightBuffer + len;
    LeaveCriticalSection(&csRecvRead);
      

  3.   

    就像打印机,当程序正在打印你的个人简历的同时,肯听不能打印VC Code
      

  4.   

    u can find more relative contents via MSDN 
    Just paste a snippet here :CCriticalSection
    An object of class CCriticalSection represents a “critical section” — a synchronization object that allows one thread at a time to access a resource or section of code. Critical sections are useful when only one thread at a time can be allowed to modify data or some other controlled resource. For example, adding nodes to a linked list is a process that should only be allowed by one thread at a time. By using a CCriticalSection object to control the linked list, only one thread at a time can gain access to the list.Critical sections are used instead of mutexes when speed is critical and the resource will not be used across process boundaries. For more information on using mutexes in MFC, see CMutex.To use a CCriticalSection object, construct the CCriticalSection object when it is needed. You can then access the critical section when the constructor returns. Call Unlock when you are done accessing the critical section.To access a resource controlled by a CCriticalSection object in this manner, first create a variable of type CSingleLock in your resource’s access member function. Then call the lock object’s Lock member function (for example, CSingleLock::Lock). At this point, your thread will either gain access to the resource, wait for the resource to be released and gain access, or wait for the resource to be released and time out, failing to gain access to the resource. In any case, your resource has been accessed in a thread-safe manner. To release the resource, use the lock object’s Unlock member function (for example, CSingleLock::Unlock), or allow the lock object to fall out of scope.Alternatively, you can create a CCriticalSection object stand-alone, and access it explicitly before attempting to access the controlled resource. This method, while clearer to someone reading your source code, is more prone to error as you must remember to lock and unlock the critical section before and after access.For more information on using CCriticalSection objects, see the articleMultithreading: How to Use the Synchronization Classes in Visual C++ Programmer's Guide.#include <afxmt.h>Class Members |  Base Class |  Hierarchy ChartSample   MFC Sample MTGDISee Also   CMutex