【多线程应用1】多线程概述及小例

时间:2022-08-21 07:41:21

多线程概念

http://baike.baidu.com/item/%E5%A4%9A%E7%BA%BF%E7%A8%8B


多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能。具有这种能力的系统包括对称多处理机多核心处理器以及芯片级多处理(Chip-level multithreading)或同时多线程(Simultaneous multithreading)处理器。 在一个程序中,这些独立运行的程序片段叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理(Multithreading)”。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程(*译作“执行绪”),进而提升整体处理性能。

【多线程应用1】多线程概述及小例

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;
}

STL中提供了std::thread类

C++ 11 ISO C++ 11 标准在STL中提供了std::thread类,因此多线程变得非常容易。
1234567891011121314 #include<thread> usingnamespacestd; voidthreadFunc(){//这里写上线程的内容}intmain(){threadt(threadFunc);//启动线程t.join();//等待线程运行完毕return0;}
一个采用了多线程技术的应用程序可以更好地利用系统资源。其主要优势在于充分利用了CPU的空闲时间片,可以用尽可能少的时间来对用户的要求做出响应,使得进程的整体运行效率得到较大提高,同时增强了应用程序的灵活性。更为重要的是,由于同一进程的所有线程是共享同一内存,所以不需要特殊的数据传送机制,不需要建立共享存储区或共享文件,从而使得不同任务之间的协调操作与运行、数据的交互、资源的分配等问题更加易于解决