zoukankan      html  css  js  c++  java
  • 线程同步问题

    // 04 线程同步的问题.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <windows.h>
    int g_n;
    
    DWORD WINAPI ThreadPro1(LPVOID lpThreadParameter){
        for (int i = 0; i < 100000; i++)
        {
            g_n++;
            //printf("我在线程1:%d
    ", g_n);
        }
            //
            //InterlockedAdd((LONG*)&g_n, 1);
        return 0;
    }
    DWORD WINAPI ThreadPro2(LPVOID lpThreadParameter){
        for (int i = 0; i < 100000; i++)
        {
            g_n++;
            //printf("我在线程2:%d
    ", g_n);
        }
            //g_n++;
            //InterlockedAdd((LONG*)&g_n, 1);
        return 0;
    }
    int _tmain(int argc, _TCHAR* argv[]){
        HANDLE hThread1 = 0, hThread2;
        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;
    }
  • 相关阅读:
    zju 2886
    zju 2478
    UVA350-水题
    UVA699-落叶-二叉树
    UVA327
    UVA548
    java环境变量
    synchronized关键字
    uva297
    UVA196
  • 原文地址:https://www.cnblogs.com/Alyoyojie/p/5317288.html
Copyright © 2011-2022 走看看