#include <windows.h>
#include <process.h>
#include <stdio.h>
#define THREAD_MAX 100000
int g_x =
unsigned __stdcall ThreadEntity(void

{
g_x++;
//InterlockedExchangeAdd(reinterpret_cast<long*>(&g_x),1); 
}
int main()

{ 
HANDLE hth[THREAD_MAX];
unsigned uiThreadID[THREAD_MAX];
printf("start create children threadings:\n");
for(int i =
{
hth = (HANDLE)_beginthreadex( NULL, // security
ThreadEntity,
(void*)&i, // arg list
&uiThreadID );
if ( hth==
printf("Failed to create thread 1\n");
}
WaitForMultipleObjects( THREAD_MAX、hth,true,10000); 
for(int i =
CloseHandle( hth );
printf("last: g_x is %d\n",g_x);
printf("Primary thread terminating.\n");
}
// Global variable indicating whether a shared resource is in use or not
BOOL g_fResourceInUse = FALSE;


void Func1() 

{
//Wait to access the resource.
Sleep(0);
//Access the resource.


//We no longer need to access the resource.
InterlockedExchange(&g_fResourceInUse、FALSE);
}
| 歡迎光臨 中央論壇 - CENTER BBS (https://www.centerbbs.com/) | Powered by Discuz! X3 |