📄 csdn_文档中心_vc++多线程下内存操作的优化.htm
字号:
<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><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>heaptest.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><o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>#define
WIN32_LEAN_AND_MEAN<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>#include
<windows.h><o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>#include
<process.h><o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>#include
<stdio.h><o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>#include
<stdlib.h><o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>/* compile with cl /MT
heaptest.c */<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>/* to switch to the system heap
issue the following command<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>before starting
heaptest from the same command line<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>set
__MSVCRT_HEAP_SELECT=__GLOBAL_HEAP_SELECTED,1
*/<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>/* structure transfers variables
to the worker threads */<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>typedef struct
tData<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>{<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>int
maximumLength;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>int
allocCount;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>}
threadData;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>void printUsage(char**
argv)<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>{<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>fprintf(stderr,"Wrong number
of parameters.\nUsage:\n");<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>fprintf(stderr,"%s threadCount maxAllocLength
allocCount\n\n",<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>argv[0]);<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>exit(1);<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>}<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>unsigned __stdcall
workerThread(void* myThreadData)<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>{<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>int
count;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>threadData*
myData;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>char*
dummy;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>srand(GetTickCount()*GetCurrentThreadId());<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>myData=(threadData*)myThreadData;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>/* now let us do
the real work */<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>for(count=0;count<myData->allocCount;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>dummy=(char*)malloc((rand()%myData->maximumLength)+1);<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>free(dummy);<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>_endthreadex(0);<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>/* to satisfy
compiler */<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><SPAN lang=EN-US>int main(int argc,char**
argv)<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US>{<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>int
threadCount;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>int
count;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>threadData
actData;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>HANDLE*
threadHandles;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>DWORD
startTime;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>DWORD
stopTime;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>DWORD
retValue;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>unsigned
dummy;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>/* check
parameters */<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>if(argc<4 ||
argc>4)<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>printUsage(argv);<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>/* get
parameters for this run */<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>threadCount=atoi(argv[1]);<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>if(threadCount>64)<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>threadCount=64;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>actData.maximumLength=atoi(argv[2])-1;<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>actData.allocCount=atoi(argv[3]);<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes">
</SPAN>threadHandles=(HANDLE*)malloc(threadCount*sizeof(HANDLE));<o:p></o:p></SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><SPAN
style="mso-spacerun: yes"> </SPAN>printf("Test run
with %d simultaneous threads:\n",threadCount);<o:p></o:p></SPAN></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -