zoukankan      html  css  js  c++  java
  • 临界区同步问题

    // 05 临界区解决同步问题.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <windows.h>
    
    int g_n;
    CRITICAL_SECTION  cs ;
    DWORD WINAPI ThreadPro1(LPVOID lpThreadParameter){
        for (int i = 0; i < 10000000; i++)
        { 
            EnterCriticalSection(&cs);
            g_n++;
            printf("我在线程1:%d
    ", g_n);
            LeaveCriticalSection(&cs);
        }
        return 0;
    }
    DWORD WINAPI ThreadPro2(LPVOID lpThreadParameter){
        for (int i = 0; i < 10000000; i++)
        {
            EnterCriticalSection(&cs);
                g_n++;
                printf("我在线程2:%d
    ", g_n);
           LeaveCriticalSection(&cs);
        }
        return 0;
    }
    int _tmain(int argc, _TCHAR* argv[]){
    
        HANDLE hThread1 = 0, hThread2;
        InitializeCriticalSection(&cs);
        hThread1 = CreateThread(NULL, NULL, ThreadPro1, NULL, NULL, NULL);
        hThread2 = CreateThread(NULL, NULL, ThreadPro2, NULL, NULL, NULL);
        WaitForSingleObject(hThread1, -1);
        WaitForSingleObject(hThread2, -1);
        printf("%d", g_n);
        return 0;
    }
  • 相关阅读:
    K-邻近算法
    算法
    (12)ubunto 快捷键
    (38)C#IIS
    RichEditControl(富文本控件)
    Gaugecontrol(测量仪器图形控件)
    鏖战字符串
    bzoj3713 [PA2014]Iloczyn|暴力(模拟)
    约会安排HDU
    hdu4614 线段树+二分 插花
  • 原文地址:https://www.cnblogs.com/Alyoyojie/p/5317291.html
Copyright © 2011-2022 走看看