多线程学习之一:线程对共享全局变量的访问
2011-04-08 22:55
435 查看
多个线程之间,可以访问共享变量,但存在风险。因为各个线程的各语句执行顺序可以是任意的,所以访问共享全局变量时需要互斥加锁,才能保证结果可靠。
在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
在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
相关文章推荐
- 关于进程和线程对于全局变量共享的问题学习总结
- java多线程学习-多个线程访问对象共享数据的方式
- 张孝祥[致敬]-多线程学习第05课-线程范围内的共享变量
- 张孝祥[致敬]-多线程学习第06课-多个线程之间的共享变量
- 关于进程和线程对于全局变量共享的问题学习总结
- C#多线程访问定义的全局变量(lock线程锁)
- 多线程之3-------多个线程访问共享数据的方式
- 多线程学习--通过单例模式来实现多个线程中的数据共享
- linux多线程应用--线程内部私有的全局变量
- java多线程的共享变量访问控制实例
- python:线程的使用、线程共享全局变量、列表作为参数(全局变量)、避免全局变量被修改
- java 多线程访问全局变量,值为null
- 8.多线程学习--使用线程本地变量
- 多线程一共就俩问题:1.线程安全(访问共享数据) 2.线程通信(wait(),notify())
- python:线程的使用、线程共享全局变量、列表作为参数(全局变量)、避免全局变量被修改
- java多线程与线程并发五:多个线程访问共享对象和数据的方式
- 并行编程之多线程共享非volatile变量,会不会可能导致线程while死循环
- 多线程学习--案例--多线程中数据共享--设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序
- 05_张孝祥_Java多线程_线程范围内共享变量的概念与作用
- Go语言学习笔记(4)-共享变量访问