小编Pr *_*r V的帖子

使用信号量的Threadsafe单例问题

我写了一个简单的单例应用程序.

以下是我的样本主类

// ThreadsafeSingletonUsingSemaphore.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include "MySingleton.h"
using namespace std;

int i =0;
#define THREADCOUNT 100
DWORD WINAPI ThreadProc(LPVOID lParam);
HANDLE g_semaphore = NULL;

int _tmain(int argc, _TCHAR* argv[])
{
    g_semaphore = CreateSemaphore(NULL,1,1,_T("TreadOne"));
    HANDLE hThread[THREADCOUNT];
    DWORD aThreadID;

    for(int iCount = 0; iCount < THREADCOUNT ; iCount++)
    {
        hThread[iCount] = CreateThread(NULL, 0, ThreadProc, 0,0, &aThreadID);
        if( hThread[iCount] == NULL )
        {
            cout<<"CreateThread error: %d" << GetLastError() …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading semaphore

4
推荐指数
1
解决办法
278
查看次数

标签 统计

c++ ×1

multithreading ×1

semaphore ×1