• 互斥体解决同步问题


    // 互斥体解决线程同步问题.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <windows.h>
    
    
    int g_n;
    HANDLE  hMutex;
    DWORD WINAPI ThreadPro1(LPVOID lpThreadParameter){
        for (int i = 0; i < 10000000; i++)
        {
            WaitForSingleObject(hMutex, -1);
            g_n++;
            printf("我在线程1:%d
    ", g_n);
            Sleep(100);
            ReleaseMutex(hMutex);
        }
        return 0;
    }
    DWORD WINAPI ThreadPro2(LPVOID lpThreadParameter){
        for (int i = 0; i < 10000000; i++)
        {
            WaitForSingleObject(hMutex, -1);
            g_n++;
            printf("我在线程2:%d
    ", g_n);
            Sleep(100);
            ReleaseMutex(hMutex);
        }
        return 0;
    }
    int _tmain(int argc, _TCHAR* argv[]){
    
        HANDLE hThread1 = 0, hThread2;
        //创建一个互斥体
        hMutex = CreateMutex(NULL, FALSE, NULL);
    
    
    
        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;
    }
  • 相关阅读:
    blob 下载功能和预览功能
    实现大文件上传
    element ui框架之Upload
    常用utils
    vue实现excel表格上传解析与导出
    理解script加载
    js处理10万条数据
    Shadow DOM
    20150625_Andriod_01_ListView1_条目显示
    20150624_Andriod _web_service_匹配
  • 原文地址:https://www.cnblogs.com/Alyoyojie/p/5317313.html
Copyright © 2020-2023  润新知