欢迎来到天天文库
浏览记录
ID:37396684
大小:781.10 KB
页数:53页
时间:2019-05-11
《多线程并行编程模型》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、多核编程大连东软信息学院多核课程组课程目录第1单元并行化程序设计基础第2单元多线程并行编程模型第3单元并行化程序性能调优第2单元主要内容2.1WindowsAPI多线程编程2.2OpenMP多线程编程第4次课程主要内容2.1WindowsAPI多线程编程创建,终止线程的方法利用同步对象协调线程的运行和内存访问资源互斥数据共享目标引例输出HelloThreadDWORDWINAPIhelloFunc(LPVOIDarg){printf(“HelloThread”);return0;}main(){HANDLEhT
2、hread=CreateThread(NULL,0,helloFunc,NULL,0,NULL);WaitForSingleObject(hThread,INFINITE);}引例定义:内核对象是由操作系统内核分配的,只能由内核访问的一个内存块,用来供系统和应用程序使用和管理各种系统资源。线程对象、事件对象、文件对象、文件映射对象、作业对象、互斥量、管道对象、进程对象、信标对象和等待计时器对象等。对象都是通过调用函数来创建的。例如CreateThread()内核对象HANDLECreateThread(LPSECU
3、RITY_ATTRIBUTESlpThreadAttributes,//在系统中使用方法进程高级设置DWORDdwStackSize,//线程堆栈大小LPTHREAD_START_ROUTINElpStartAddress,//函数指针,指向实际运行的代码LPVOIDlpParameter,//参数指针DWORDdwCreationFlags,//设置标志LPDWORDlpThreadId);//线程ID创建线程LPTHREAD_START_ROUTINEDWORDWINAPIMyThreadStart(LPVOI
4、Dp);创建线程BOOLCloseHandle(HANDLEhObject);线程的终止#include#includeDWORDWINAPIhelloFunc(LPVOIDarg){printf(“HelloThread”);return0;}main(){HANDLEhThread=CreateThread(NULL,0,helloFunc,NULL,0,NULL);}WhatHappens?主线程执行太快,子线程没有执行例子:线程的创建#include5、h>#includeBOOLthreadDone=FALSE;DWORDWINAPIhelloFunc(LPVOIDarg){printf(“HelloThread”);threadDone=TRUE;return0;}main(){HANDLEhThread=CreateThread(NULL,0,helloFunc,NULL,0,NULL);while(!threadDone);//wastedcycles!}Notagoodidea!线程的等待原型:等待一个线程DWORDWaitFo6、rSingleObject(HANDLEhHandle,DWORDdwMilliseconds);线程的等待引例输出:HelloThreadHelloThreadHelloThreadHelloThread原型TheWaitForMultipleObjects()functionhasthefollowingparameters:nCountlpHandlesfWaitAlldwMillisecondsDWORDWaitForMultipleObjects(DWORDnCount,CONSTHANDLE*lpHan7、dles,//arrayBOOLfWaitAll,//waitforoneorallDWORDdwMilliseconds);Waitforall:fWaitAll==TRUEWaitforany:fWaitAll==FALSE多个线程的等待constintnumThreads=4;DWORDWINAPIhelloFunc(LPVOIDarg){printf(“HelloThread”);return0;}main(){HANDLEhThread[numThreads];for(inti=0;i8、ads;i++)hThread[i]=CreateThread(NULL,0,helloFunc,NULL,0,NULL);WaitForMultipleObjects(numThreads,hThread,TRUE,INFINITE)}引例解决方案问题思考输出:HelloThread0HelloThread1HelloThread2HelloThread3
5、h>#includeBOOLthreadDone=FALSE;DWORDWINAPIhelloFunc(LPVOIDarg){printf(“HelloThread”);threadDone=TRUE;return0;}main(){HANDLEhThread=CreateThread(NULL,0,helloFunc,NULL,0,NULL);while(!threadDone);//wastedcycles!}Notagoodidea!线程的等待原型:等待一个线程DWORDWaitFo
6、rSingleObject(HANDLEhHandle,DWORDdwMilliseconds);线程的等待引例输出:HelloThreadHelloThreadHelloThreadHelloThread原型TheWaitForMultipleObjects()functionhasthefollowingparameters:nCountlpHandlesfWaitAlldwMillisecondsDWORDWaitForMultipleObjects(DWORDnCount,CONSTHANDLE*lpHan
7、dles,//arrayBOOLfWaitAll,//waitforoneorallDWORDdwMilliseconds);Waitforall:fWaitAll==TRUEWaitforany:fWaitAll==FALSE多个线程的等待constintnumThreads=4;DWORDWINAPIhelloFunc(LPVOIDarg){printf(“HelloThread”);return0;}main(){HANDLEhThread[numThreads];for(inti=0;i8、ads;i++)hThread[i]=CreateThread(NULL,0,helloFunc,NULL,0,NULL);WaitForMultipleObjects(numThreads,hThread,TRUE,INFINITE)}引例解决方案问题思考输出:HelloThread0HelloThread1HelloThread2HelloThread3
8、ads;i++)hThread[i]=CreateThread(NULL,0,helloFunc,NULL,0,NULL);WaitForMultipleObjects(numThreads,hThread,TRUE,INFINITE)}引例解决方案问题思考输出:HelloThread0HelloThread1HelloThread2HelloThread3
此文档下载收益归作者所有