📄 csdn_文档中心_vc++多线程下内存操作的优化.htm
字号:
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>startTime=GetTickCount();<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>for(count=0;count<threadCount;count++)<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>{<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>threadHandles[count]=(HANDLE)_beginthreadex(0,0,<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>&workerThread,
(void*)&actData,0,&dummy);<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>if(threadHandles[count]==(HANDLE)-1)<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>{<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>fprintf(stderr,"Error starting worker
threads.\n");<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN><SPAN
style="mso-spacerun: yes"> </SPAN>exit(2);<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>/* wait until
all threads are done */<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>retValue=WaitForMultipleObjects(threadCount,threadHandles<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>,1,INFINITE);<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>stopTime=GetTickCount();<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>printf("Total
time elapsed was: %d milliseconds",<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>stopTime-startTime);<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>printf(" for %d
alloc operations.\n",<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>actData.allocCount*threadCount);<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>/* cleanup
*/<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>for(count=0;count<threadCount;count++)<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>CloseHandle(threadHandles[count]);<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>free(threadHandles);<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>return
0;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">测试程序在处理完参数后,创建参数</SPAN><SPAN
lang=EN-US>1</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">指定数量的线程,</SPAN><SPAN
lang=EN-US>threadData</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">结构用于传递计数变量。</SPAN><SPAN
lang=EN-US>workThread</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中进行内存操作,首先初始化随机数发生器,然后进行指定数量的</SPAN><SPAN
lang=EN-US>malloc()</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN
lang=EN-US>free()</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">操作。主线程调用</SPAN><SPAN
lang=EN-US>WaitForMultipleObject()</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">等待工作者线程结束,然后输出线程运行的时间。计时不是十分精确,但影响不大。</SPAN><SPAN
lang=EN-US><o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">为了编译这个程序,需要已经安装</SPAN><SPAN
lang=EN-US>VC++ v6.0</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序,打开一个命令行窗口,键入以下命令:</SPAN><SPAN
lang=EN-US><o:p></o:p></SPAN></P>
<P class=MsoNormal
style="TEXT-INDENT: 21pt; mso-char-indent-count: 2.0; mso-char-indent-size: 10.5pt"><SPAN
lang=EN-US>cl /MT heaptest.c<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>/MT</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">表示同</SPAN><SPAN
lang=EN-US>C</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">运行库的多线程版静态链接。如果要动态链接,用</SPAN><SPAN
lang=EN-US>/MD</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。如果</SPAN><SPAN
lang=EN-US>VC++</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是</SPAN><SPAN
lang=EN-US>v5.0</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的话并且有高版本的</SPAN><SPAN
lang=EN-US>msvcrt.dll</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,应该用动态链接。现在运行这个程序,用性能监视器查看线程切换的次数,然后按上面设置环境参数,重新运行这个程序,再次查看线程切换次数。</SPAN><SPAN
lang=EN-US><o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">当截取这两张图时,测试程序用了</SPAN><SPAN
lang=EN-US>60,953ms</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">进行了</SPAN><SPAN
lang=EN-US>3,000,000</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">次的内存申请操作,使用的是</SPAN><SPAN
lang=EN-US>VC++ v6</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的堆操作函数。在转换使用</SPAN><SPAN
lang=EN-US>HeapAlloc()</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">后,同样的操作仅用了</SPAN><SPAN
lang=EN-US>5,291ms</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。在这个特定的情况下,使用</SPAN><SPAN
lang=EN-US>HeapAlloc()</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">使得性能提高了</SPAN><SPAN
lang=EN-US>10</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">倍以上!在实际的程序同样可以看到这种性能的提升。</SPAN><SPAN
lang=EN-US><o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal><B><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">结论</SPAN><SPAN
lang=EN-US><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="TEXT-INDENT: 21pt"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">多处理器系统可以自然提升程序的性能,但如果发生多个处理器争用同一个资源,则可能多处理器的系统的性能还不如单处理器系统。对于</SPAN><SPAN
lang=EN-US>C/C++</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序,问题通常发生在当多个线程进行频繁的内存操作活动时。如上文所述,只要进行很少的一些设置,就可能极大地提高多线程程序在多处理器下的性能。这种方法即不需要源程序,也不需要重新编译可执行文件,而最大的好处是用这种方法得到的性能的提高是不用支付任何费用的。</SPAN></P><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE align=center bgColor=#006699 border=0 cellPadding=0 cellSpacing=0
width=770>
<TBODY>
<TR bgColor=#006699>
<TD align=middle bgColor=#006699 id=white><FONT
color=#ffffff>对该文的评论</FONT></TD>
<TD align=middle>
<SCRIPT src="CSDN_文档中心_VC++多线程下内存操作的优化.files/readnum.htm"></SCRIPT>
</TD></TR></TBODY></TABLE>
<TABLE align=center bgColor=#666666 border=0 cellPadding=2 cellSpacing=1
width=770>
<TBODY>
<TR>
<TD bgColor=#cccccc colSpan=3><SPAN style="COLOR: #cccccc"><IMG height=16
hspace=1 src="CSDN_文档中心_VC++多线程下内存操作的优化.files/ico_pencil.gif" width=16>
</SPAN> doose <I>(2001-4-20 21:45:52)</I> </TD></TR>
<TR>
<TD bgColor=#ffffff colSpan=3 width=532><BR>就是爽啊
<BR></TD></TR></TBODY></TABLE><BR>
<DIV align=center>
<TABLE align=center bgColor=#cccccc border=0 cellPadding=2 cellSpacing=1
width=770>
<TBODY>
<TR>
<TH bgColor=#00
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -