2.线程库
1)C++11的Boost线程库,C++11线程的头文件是
2)pthread库,定义了创建和操纵线程的一整套API。线程的头文件是
,在类unix和windows系统中都可用。
3)WIN的线程API函数,线程的头文件是
本文主要记录win线程API函数。
3.多线程优缺点
4.多线程应用
为了提高CPU的使用率,采用多线程的方式去同时完成几件事情而互不干扰,如当前进程要完成三件事情1、2、3,那么CPU会分别用10%的时间来同时处理这3件事情,从而让CPU的使用率达到了30%,大大地提高了CPU的利用率。
大多情况下,要用到多线程的主要是需要处理大量的IO操作时或处理的情况需要花大量的时间等等,比如:读写文件、视频图像的采集、处理、显示、保存;服务端的监听等;耗时或大量占用处理器的任务阻塞用户界面操作;各个任务必须等待外部资源(如远程文件或 INTERNET 连接)。
5.线程通信
1)线程间通信
6.线程示例
#include
#include
using namespace std;
CRITICAL_SECTION cs;
DWORD WINAPI ThreadFunc1(PVOID param)
{
for(int i=1;i<=10;i++) {
EnterCriticalSection(&cs);
cout<<"ThreadFunc1 Output:"<return 0;
}
DWORD WINAPI ThreadFunc2(PVOID param)
{
for(int i=1;i<=10;i++) {
EnterCriticalSection(&cs);
cout<<"ThreadFunc2 Output:"<return 0;
}
DWORD WINAPI ThreadFunc3(PVOID param)
{
for(int i=1;i<=10;i++) {
EnterCriticalSection(&cs);
cout<<"ThreadFunc3 Output:"<return 0;
}
int main()
{
InitializeCriticalSection(&cs);
HANDLE ThreadHandle1 = CreateThread(NULL,0,ThreadFunc1,NULL,0,NULL);
HANDLE ThreadHandle2 = CreateThread(NULL,0,ThreadFunc2,NULL,0,NULL);
HANDLE ThreadHandle3 = CreateThread(NULL,0,ThreadFunc3,NULL,0,NULL);
HANDLE hThread[3] = {ThreadHandle1,ThreadHandle2,ThreadHandle3};
WaitForMultipleObjects(2,hThread,TRUE,INFINITE);
DeleteCriticalSection(&cs);
CloseHandle(ThreadHandle1 );
CloseHandle(ThreadHandle2);
CloseHandle(ThreadHandle3);
return 0;
}
#include
#include
using namespace std;
HANDLE hMutex;
DWORD WINAPI ThreadFun(LPVOID lpParamter)
{
while(1)
{
WaitForSingleObject(hMutex, INFINITE);
cout<<"Fun!"<500);
ReleaseMutex(hMutex);
}
}
int main()
{
HANDLE hThread = CreateThread(NULL, 0, ThreadFun, NULL, 0, NULL);
hMutex = CreateMutex(NULL, FALSE, "screen");
while(1)
{
WaitForSingleObject(hMutex, INFINITE);
cout<<"main!"<500);
ReleaseMutex(hMutex);
}
CloseHandle(hThread);
CloseHandle(hMutex);
return 0;
}
#include
#include
using namespace std;
HANDLE hsemp1,hsemp2;
const int th_number = 2;
int i1=0,i2=0;
DWORD WINAPI ThreadFun1(LPVOID lpParameter)
{
for(int i=0;i<10;i++)
{
WaitForSingleObject(hsemp1,INFINITE);//等待信号量
cout<<"ThreadFun1->"<1,NULL);//释放信号量
}
return 0;
}
DWORD WINAPI ThreadFun2(LPVOID lpParameter)
{
for(int i=0;i<10;i++)
{
WaitForSingleObject(hsemp2,INFINITE);//等待信号量
cout<<"ThreadFun2->"<500);
ReleaseSemaphore(hsemp2,1,NULL);//释放信号量
}
return 0;
}
int main()
{
hsemp1 = CreateSemaphore(NULL,1,1,"screen");
hsemp2 = CreateSemaphore( NULL,1,1,"screen");
HANDLE hThread[th_number] = {0};
hThread[0] = CreateThread(NULL,0,ThreadFun1,NULL,0,NULL);
hThread[1] = CreateThread(NULL,0,ThreadFun2,NULL,0,NULL);
WaitForMultipleObjects(2,hThread,TRUE,INFINITE);
CloseHandle( hThread[0] );
CloseHandle( hThread[1] );
CloseHandle( hsemp1 );
CloseHandle( hsemp2 );
return 0;
}
#include <iostream>
#include <windows.h>
using namespace std;
HANDLE evRead,evWrite;
DWORD WINAPI ReadThread(PVOID param)
{
WaitForSingleObject (evRead,INFINITE);
cout<<"Reading"<<endl;
SetEvent(evWrite);
return 0;
}
DWORD WINAPI WriteThread(PVOID param)
{
cout<<"Writing"<<endl;
SetEvent(evRead);
return 0;
}
int main()
{
evRead = CreateEvent (NULL ,FALSE ,FALSE ,NULL) ;
evWrite = CreateEvent (NULL ,FALSE ,FALSE ,NULL) ;
HANDLE th1=CreateThread(NULL,0, ReadThread,NULL,0,NULL);
HANDLE th2=CreateThread(NULL,0, WriteThread,NULL,0,NULL);
WaitForSingleObject(evWrite,INFINITE) ;
CloseHandle(evRead);
CloseHandle(evWrite );
CloseHandle(th1);
CloseHandle(th2);
return 0 ;
}