1 多线程概念
http://baike.baidu.com/item/%E5%A4%9A%E7%BA%BF%E7%A8%8B
多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能。具有这种能力的系统包括对称多处理机、多核心处理器以及芯片级多处理(Chip-level multithreading)或同时多线程(Simultaneous multithreading)处理器。 在一个程序中,这些独立运行的程序片段叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理(Multithreading)”。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程(*译作“执行绪”),进而提升整体处理性能。
2 多线程实例程序-1
// 线程实验1.cpp : 定义控制台应用程序的入口点。
//http://www.jizhuomi.com/software/287.html
//http://www.doc88.com/p-9837156707682.html
//http://blog.163.com/zhangqiang4002@126/blog/static/107273688201032304124737/
#include "stdafx.h"
// 多线程编程实例1:
//#include <iostream>
//#include <windows.h>
//using namespace std;
//
//DWORD WINAPI Fun(LPVOID lpParamter)
//{
//while(1) { cout<<"Fun display!"<<endl; }
//}
//
//int main()
//{
//HANDLE hThread = CreateThread(NULL, 0, Fun, NULL, 0, NULL);
//CloseHandle(hThread);
//while(1) { cout<<"main display!"<<endl; }
//return 0;
//}
//多线程编程实例2:
//#include <iostream>
//#include <windows.h>
//using namespace std;
//
//DWORD WINAPI Fun(LPVOID lpParamter)
//{
//while(1) { cout<<"Fun display!"<<endl; Sleep(1000);}
//}
//
//int main()
//{
//HANDLE hThread = CreateThread(NULL, 0, Fun, NULL, 0, NULL);
//CloseHandle(hThread);
//while(1) { cout<<"main display!"<<endl; Sleep(2000);}
//return 0;
//}
//
//// 多线程编程实例3:
//
//#include <iostream>
//#include <windows.h>
//using namespace std;
//
//DWORD WINAPI Fun(LPVOID lpParamter)
//{
//while(1) { cout<<"Fun display!\n"; Sleep(1000);} //<<endl \n
//}
//
//int main()
//{
//HANDLE hThread = CreateThread(NULL, 0, Fun, NULL, 0, NULL);
//CloseHandle(hThread);
//while(1) { cout<<"main display!\n"; Sleep(2000);}
//return 0;
//}
//
//
//// 多线程编程实例4:
//
//
////多线程编程实例5:
//#include <iostream>
//#include <windows.h>
//using namespace std;
//
//HANDLE hMutex;
//
//DWORD WINAPI Fun(LPVOID lpParamter)
//{
//while(1) {
//WaitForSingleObject(hMutex, INFINITE);
//cout<<"Fun display!"<<endl;
//Sleep(1000);
//ReleaseMutex(hMutex);
//}
//}
//
//int main()
//{
//HANDLE hThread = CreateThread(NULL, 0, Fun, NULL, 0, NULL);
//hMutex = CreateMutex(NULL, FALSE, "screen");
//CloseHandle(hThread);
//while(1) {
//WaitForSingleObject(hMutex, INFINITE);
//cout<<"main display!"<<endl;
//Sleep(2000);
//ReleaseMutex(hMutex);
//}
//
//return 0;
//}
3多线程实例程序-2
//http://daimajishu.iteye.com/blog/1086595#
//这是2个线程模拟卖火车票的小程序
#include <windows.h>
//#include <iostream.h>
#include <iostream>
using namespace std;
DWORD WINAPI Fun1Proc(LPVOID lpParameter);//thread data
DWORD WINAPI Fun2Proc(LPVOID lpParameter);//thread data
int index=0;
int tickets=10;
HANDLE hMutex;
void main()
{
HANDLE hThread1;
HANDLE hThread2;
//创建线程
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
//创建互斥对象
hMutex=CreateMutex(NULL,TRUE,"tickets");
if (hMutex)
{
if (ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only one instance can run!"<<endl;
return;
}
}
WaitForSingleObject(hMutex,INFINITE);
ReleaseMutex(hMutex);
ReleaseMutex(hMutex);
Sleep(4000);
}
//线程1的入口函数
DWORD WINAPI Fun1Proc(LPVOID lpParameter)//thread data
{
while (true)
{
ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if (tickets>0)
{
Sleep(1);
cout<<"thread1 sell ticket :"<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}
return 0;
}
//线程2的入口函数
DWORD WINAPI Fun2Proc(LPVOID lpParameter)//thread data
{
while (true)
{
ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if (tickets>0)
{
Sleep(1);
cout<<"thread2 sell ticket :"<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}
return 0;
}
4 STL中提供了std::thread类
C++ 11 ISO C++ 11 标准在STL中提供了std::thread类,因此多线程变得非常容易。1234567891011121314 | #include<thread> usingnamespacestd; voidthreadFunc(){ //这里写上线程的内容 } intmain(){ threadt(threadFunc); //启动线程 t.join(); //等待线程运行完毕 return0; } |