欢迎来到天天文库
浏览记录
ID:59174352
大小:85.50 KB
页数:11页
时间:2020-10-30
《大连东软信息学院-多核多线程-实验一.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、项目一:Windows*Threads多线程编程模块一:基础模块4编译执行,输出结果:简答与思考:1写出修改后的HelloThreads的代码。//HelloThreads.cpp:定¡§义°?控?制?台¬¡§应®
2、用®?程¨¬序¨°的Ì?入¨?口¨²点Ì?。¡ê#include"stdafx.h"#includeconstintnumThreads=4;DWORDWINAPIhelloFunc(LPVOIDarg){intmyNum=*((int*)arg);printf("HelloThr
3、ead!Thread%d",myNum);return0;}intmain(intargc,_TCHAR*argv[]){HANDLEhThread[numThreads];inttNum[numThreads];for(inti=0;i4、TE);return0;}1项目总结。在主线程中循环调用CreateThread()函数生成四个子线程分别去执行helloFunc()函数,并将线程的句柄保存在hThread数组中,CreateThread()的第四个参数给每个线程传递参数tNum获取该线程号,由helloFunc()的参数接收并指向内容。由于主线程为每个子线程传递了不同的参数,所以通过子线程的执行结果可以判断出四个线程的执行顺序。这个实验要我们初步认识了程序的并行化设计思想,为以后的并行化程序设计打下了基础。2模块二:临界区模块3编译执行,记录结5、果:Pi=Thetimeofcalculationwasseconds6编译执行,记录结果:Pi=Thetimeofcalculationwasseconds7加速比:1.,并行效率:0.简答与思考:1如何进行并行化的?请写出并行化的思路与具体的代码。答:在程序中创建两个子线程,另主线程等待所有的子线程执行结束后退出。代码:#include"stdafx.h"#include#includestaticlongnum_steps=;constintgNumThreads=2;d6、oublestep=0.0,pi=0.0,sum=0.0;CRITICAL_SECTIONgCS;DWORDWINAPIthreadFunction(LPVOIDpArg){intmyNum=*((int*)pArg);doublesum1=0.0,x;for(inti=myNum;i7、Section(&gCS);return0;}intmain(intargc,_TCHAR*argv[]){clock_tstart,stop;start=clock();HANDLEthreadHandles[gNumThreads];inttNum[gNumThreads];InitializeCriticalSection(&gCS);step=1.0/(double)num_steps;for(inti=0;i8、teThread(NULL,0,threadFunction,&tNum[i],0,NULL);}WaitForMultipleObjects(gNumThreads,threadHandles,TRUE,INFINITE);DeleteCriticalSection(&gCS);pi=step*sum;stop=clock();printf("Pi=%12.12f",pi);printf("Thetimeofcalculationwas%fseconds",((double)(stop-start)/109、00.0));return0;}1在本模块中,哪些变量是需要保护的?为什么?采取什么方法实现的?答:1)临界资源是需要保护的,也就是sum变量,这里将对sum的更新操作定义为临界区操作2)在多线程程序中,对于在子线程执行的代码中出现的被更新的全局变量要多加考虑,因为具有这种特点的变量,很有可能会引起数据冲突,如果处理不好会使程序产生功能性错误3)EnterC
4、TE);return0;}1项目总结。在主线程中循环调用CreateThread()函数生成四个子线程分别去执行helloFunc()函数,并将线程的句柄保存在hThread数组中,CreateThread()的第四个参数给每个线程传递参数tNum获取该线程号,由helloFunc()的参数接收并指向内容。由于主线程为每个子线程传递了不同的参数,所以通过子线程的执行结果可以判断出四个线程的执行顺序。这个实验要我们初步认识了程序的并行化设计思想,为以后的并行化程序设计打下了基础。2模块二:临界区模块3编译执行,记录结
5、果:Pi=Thetimeofcalculationwasseconds6编译执行,记录结果:Pi=Thetimeofcalculationwasseconds7加速比:1.,并行效率:0.简答与思考:1如何进行并行化的?请写出并行化的思路与具体的代码。答:在程序中创建两个子线程,另主线程等待所有的子线程执行结束后退出。代码:#include"stdafx.h"#include#includestaticlongnum_steps=;constintgNumThreads=2;d
6、oublestep=0.0,pi=0.0,sum=0.0;CRITICAL_SECTIONgCS;DWORDWINAPIthreadFunction(LPVOIDpArg){intmyNum=*((int*)pArg);doublesum1=0.0,x;for(inti=myNum;i7、Section(&gCS);return0;}intmain(intargc,_TCHAR*argv[]){clock_tstart,stop;start=clock();HANDLEthreadHandles[gNumThreads];inttNum[gNumThreads];InitializeCriticalSection(&gCS);step=1.0/(double)num_steps;for(inti=0;i8、teThread(NULL,0,threadFunction,&tNum[i],0,NULL);}WaitForMultipleObjects(gNumThreads,threadHandles,TRUE,INFINITE);DeleteCriticalSection(&gCS);pi=step*sum;stop=clock();printf("Pi=%12.12f",pi);printf("Thetimeofcalculationwas%fseconds",((double)(stop-start)/109、00.0));return0;}1在本模块中,哪些变量是需要保护的?为什么?采取什么方法实现的?答:1)临界资源是需要保护的,也就是sum变量,这里将对sum的更新操作定义为临界区操作2)在多线程程序中,对于在子线程执行的代码中出现的被更新的全局变量要多加考虑,因为具有这种特点的变量,很有可能会引起数据冲突,如果处理不好会使程序产生功能性错误3)EnterC
7、Section(&gCS);return0;}intmain(intargc,_TCHAR*argv[]){clock_tstart,stop;start=clock();HANDLEthreadHandles[gNumThreads];inttNum[gNumThreads];InitializeCriticalSection(&gCS);step=1.0/(double)num_steps;for(inti=0;i8、teThread(NULL,0,threadFunction,&tNum[i],0,NULL);}WaitForMultipleObjects(gNumThreads,threadHandles,TRUE,INFINITE);DeleteCriticalSection(&gCS);pi=step*sum;stop=clock();printf("Pi=%12.12f",pi);printf("Thetimeofcalculationwas%fseconds",((double)(stop-start)/109、00.0));return0;}1在本模块中,哪些变量是需要保护的?为什么?采取什么方法实现的?答:1)临界资源是需要保护的,也就是sum变量,这里将对sum的更新操作定义为临界区操作2)在多线程程序中,对于在子线程执行的代码中出现的被更新的全局变量要多加考虑,因为具有这种特点的变量,很有可能会引起数据冲突,如果处理不好会使程序产生功能性错误3)EnterC
8、teThread(NULL,0,threadFunction,&tNum[i],0,NULL);}WaitForMultipleObjects(gNumThreads,threadHandles,TRUE,INFINITE);DeleteCriticalSection(&gCS);pi=step*sum;stop=clock();printf("Pi=%12.12f",pi);printf("Thetimeofcalculationwas%fseconds",((double)(stop-start)/10
9、00.0));return0;}1在本模块中,哪些变量是需要保护的?为什么?采取什么方法实现的?答:1)临界资源是需要保护的,也就是sum变量,这里将对sum的更新操作定义为临界区操作2)在多线程程序中,对于在子线程执行的代码中出现的被更新的全局变量要多加考虑,因为具有这种特点的变量,很有可能会引起数据冲突,如果处理不好会使程序产生功能性错误3)EnterC
此文档下载收益归作者所有