码迷,mamicode.com
首页 > 编程语言 > 详细

C++多线程2

时间:2017-01-01 18:57:43      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:long   pause   rlock   bsp   线程   nbsp   code   eth   div   

#include "stdafx.h"
#include <windows.h>

int g_count;
const int ThreadNum = 10;
DWORD __stdcall Func(LPVOID pm)
{
    Sleep(10);
    g_count++;
    //InterlockedIncrement((LPLONG)&g_count);
    return 0;
}

int main()
{
    HANDLE  handle[ThreadNum];
    for (int k = 0; k < ThreadNum; ++k)
    {
        g_count = 0;
        for (int i = 0; i < ThreadNum; ++i)
        {
            handle[i] = CreateThread(NULL, 0, Func, (LPVOID)i, 0, NULL);
        }
        WaitForMultipleObjects(ThreadNum, handle, TRUE, INFINITE);
        printf_s("%d\n", g_count);
    }
    system("pause");
    return 0;
}

 

C++多线程2

标签:long   pause   rlock   bsp   线程   nbsp   code   eth   div   

原文地址:http://www.cnblogs.com/July7th/p/6241301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!