多个线程之间,可以访问共享变量,但存在风险。因为各个线程的各语句执行顺序可以是任意的,所以访问共享全局变量时需要互斥加锁,才能保证结果可靠。
在VC中,线程操作的几个接口函数:
线程相关:
创建线程:
CreateThread(...);
等待线程结束:
WaitForMultipleObjects(...);
获取当前线程Id:
GetCurrentThreadId(void);
Mutex相关:
创建Mutex:
CreateMutex(...);
等待请utex:
WaitForSingleObject(handle,time);
释放Mutex:
ReleaseMutex(mutex);
程序1:不加互斥,全局变量结果不确定;
程序2:加了互斥,全局变量结果确定。
附:
程序1:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <WINBASE.h>
#define MAX_NUM 100
int count =0;
unsigned long thread_func(void* pArg);
int main()
{
unsigned long tid[2] = {0};
unsigned long selfTid = 0;
HANDLE handle[2] = {NULL};
selfTid = GetCurrentThreadId();
printf("enter main tid: %u/r/n",selfTid);
count = 0;
handle[0] = CreateThread(NULL,2048,(LPTHREAD_START_ROUTINE)thread_func,NULL,NULL,&tid[0]);
handle[1] = CreateThread(NULL,2048,(LPTHREAD_START_ROUTINE)thread_func,NULL,NULL,&tid[1]);
WaitForMultipleObjects(2,handle,TRUE,0xFFFFFFFF);
if(count == 2 * MAX_NUM)
{
printf("OK,count is: %u/r/n",count);
}
else
{
printf("why?count is: %u/r/n",count);
}
printf("tid[0]: %u,tid[1]:%u/r/n",tid[0],tid[1]);
printf("return from main tid: %u/r/n",selfTid);
CloseHandle(handle[0]);
CloseHandle(handle[1]);
return 0;
}
unsigned long thread_func(void* pArg)
{
int i=0;
unsigned long tid =0;
int iTmp =0;
tid =GetCurrentThreadId();
printf("enter tid: %u/r/n",tid);
for(i=0; i<MAX_NUM; i++)
{
iTmp = count +1;
//Sleep(20);
for(int j=0;j<1000000;j++);
count = iTmp;
}
printf("return from tid: %u/r/n",tid);
return 0;
}
运行结果1:
enter main tid: 3588
enter tid: 1884
enter tid: 300
return from tid: 1884
return from tid: 300
why?count is: 121
tid[0]: 1884,tid[1]:300
return from main tid: 3588
Press any key to continue
运行结果2:
enter main tid: 3856
enter tid: 3048
enter tid: 3396
return from tid: 3048
return from tid: 3396
why?count is: 122
tid[0]: 3048,tid[1]:3396
return from main tid: 3856
Press any key to continue
程序2:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <WINBASE.h>
#define MAX_NUM 100
int count =0;
unsigned long thread_func(void* pArg);
HANDLE hMutex =0;
int main()
{
unsigned long tid[2] = {0};
unsigned long selfTid = 0;
HANDLE handle[2] = {NULL};
//hMutex = OpenMutex(NULL,FALSE,"mutext02");
if(NULL == hMutex)
{
hMutex = CreateMutex(NULL,FALSE,"mutext02");
}
selfTid = GetCurrentThreadId();
printf("enter main tid: %u,mutex;%u/r/n",selfTid,hMutex);
count = 0;
handle[0] = CreateThread(NULL,2048,(LPTHREAD_START_ROUTINE)thread_func,NULL,NULL,&tid[0]);
handle[1] = CreateThread(NULL,2048,(LPTHREAD_START_ROUTINE)thread_func,NULL,NULL,&tid[1]);
WaitForMultipleObjects(2,handle,TRUE,INFINITE);
if(count == 2 * MAX_NUM)
{
printf("OK,count is: %u/r/n",count);
}
else
{
printf("why?count is: %u/r/n",count);
}
printf("tid[0]: %u,tid[1]:%u/r/n",tid[0],tid[1]);
printf("return from main tid: %u/r/n",selfTid);
CloseHandle(handle[0]);
CloseHandle(handle[1]);
CloseHandle(hMutex);
return 0;
}
unsigned long thread_func(void* pArg)
{
int i=0;
unsigned long tid =0;
int iTmp =0;
tid =GetCurrentThreadId();
printf("enter tid: %u,mutex;%u/r/n",tid,hMutex);
for(i=0; i<MAX_NUM; i++)
{
WaitForSingleObject(hMutex,INFINITE);
iTmp = count +1;
Sleep(20);
for(int j=0;j<1000000;j++);
count = iTmp;
ReleaseMutex(hMutex);
}
printf("return from tid: %u/r/n",tid);
return 0;
}
运行结果:
enter main tid: 2704,mutex;2032
enter tid: 1580,mutex;2032
enter tid: 3392,mutex;2032
return from tid: 1580
return from tid: 3392
OK,count is: 200
tid[0]: 1580,tid[1]:3392
return from main tid: 2704
Press any key to continue