多线程式同步之互斥技术的应用
2007-03-05 15:10
197 查看
我们还是用上面的那个程序,要实现数据有序的读写,这次我们用互斥技术.
example1.c
#include<iostream.h>
#include<process.h>
#include<windows.h>
#include<stdio.h>
int a[5];
DWORD WINAPI Thread1(LPVOID lpParamter)
{
while(true)
{
for(int i = 0; i<5; i++)
{
a[i] = i ;
}
}
return 0 ;
}
void main()
{
HANDLE thread1;
DWORD d1;
thread1 = CreateThread(NULL,0,Thread1,NULL,0,&d1);
if(thread1 == NULL)
{
cout<<"create thread fail......"<<endl;
}
while(1)
{
for(int i = 0; i<5; i++)
{
cout<<a[0]<<" ";
}
cout<<endl<<""<<endl;
}
CloseHandle( thread1 );
example2.c
#include<iostream.h>
#include<process.h>
#include<windows.h>
#include<stdio.h>
int a[5];
HANDLE hMutex ;
DWORD WINAPI Thread1(LPVOID lpParamter)
{
while(true)
{
WaitForSingleObject(hMutex,INFINITE);
for(int i = 0; i<5; i++)
{
a[i] = i ;
}
ReleaseMutex(hMutex);
}
return 0 ;
}
void main()
{
HANDLE thread1;
DWORD d1;
hMutex = CreateMutex(NULL,FALSE,NULL);
thread1 = CreateThread(NULL,0,Thread1,NULL,0,&d1);
if(thread1 == NULL)
{
cout<<"create thread fail......"<<endl;
}
while(1)
{
WaitForSingleObject(hMutex,INFINITE);
for(int i = 0; i<5; i++)
{
cout<<a[0]<<" ";
}
cout<<endl<<""<<endl;
ReleaseMutex(hMutex);
}
CloseHandle( thread1 );
example1.c
#include<iostream.h>
#include<process.h>
#include<windows.h>
#include<stdio.h>
int a[5];
DWORD WINAPI Thread1(LPVOID lpParamter)
{
while(true)
{
for(int i = 0; i<5; i++)
{
a[i] = i ;
}
}
return 0 ;
}
void main()
{
HANDLE thread1;
DWORD d1;
thread1 = CreateThread(NULL,0,Thread1,NULL,0,&d1);
if(thread1 == NULL)
{
cout<<"create thread fail......"<<endl;
}
while(1)
{
for(int i = 0; i<5; i++)
{
cout<<a[0]<<" ";
}
cout<<endl<<""<<endl;
}
CloseHandle( thread1 );
example2.c
#include<iostream.h>
#include<process.h>
#include<windows.h>
#include<stdio.h>
int a[5];
HANDLE hMutex ;
DWORD WINAPI Thread1(LPVOID lpParamter)
{
while(true)
{
WaitForSingleObject(hMutex,INFINITE);
for(int i = 0; i<5; i++)
{
a[i] = i ;
}
ReleaseMutex(hMutex);
}
return 0 ;
}
void main()
{
HANDLE thread1;
DWORD d1;
hMutex = CreateMutex(NULL,FALSE,NULL);
thread1 = CreateThread(NULL,0,Thread1,NULL,0,&d1);
if(thread1 == NULL)
{
cout<<"create thread fail......"<<endl;
}
while(1)
{
WaitForSingleObject(hMutex,INFINITE);
for(int i = 0; i<5; i++)
{
cout<<a[0]<<" ";
}
cout<<endl<<""<<endl;
ReleaseMutex(hMutex);
}
CloseHandle( thread1 );
相关文章推荐
- 多线程并发库高级应用 之 传统定时器、互斥、同步通信技术
- 多线程并发库高级应用 之 传统定时器、互斥、同步通信技术
- 多线程并发库高级应用 之 传统定时器、互斥、同步通信技术
- 多线程并发库高级应用 之 传统定时器、互斥、同步通信技术
- WMI技术介绍和应用——使用VC编写一个半同步查询WMI服务的类
- 转载:基于NTP的高精度时钟同步系统实现_电子技术应用
- 多线程并发库高级应用 之 使用java5中同步技术的3个面试题
- Linux互斥与同步应用(四):posix信号量的互斥与同步
- Domino分布式数据库同步复制技术在广东粤华发电有限责任公司的企业信息平台上成功应用
- java 同步通信技术的应用
- 数据实时同步或抽取上收的技术分析(社保、电力营销、财政、税务征管、公安警务等地市数据省级大集中应用/异地灾备)
- SQL SERVER 2005同步复制技术的应用
- Java核心技术学习---多线程,锁,同步,条件对象部分应用源码
- Linux互斥与同步应用(三):posix线程实现单个生产者和单个消费者模型
- SQL SERVER 2005同步复制技术的应用
- 3、线程的同步互斥技术和通信
- Linux互斥与同步应用(五):system V信号量的互斥与同步
- Linux互斥与同步应用(二):posix线程同步
- Java高并发编程:定时器、互斥、同步通信技术