﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-忍者的垃圾堆-文章分类-Visual C++</title><link>http://www.cppblog.com/renzhe/category/392.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 20 May 2008 20:00:18 GMT</lastBuildDate><pubDate>Tue, 20 May 2008 20:00:18 GMT</pubDate><ttl>60</ttl><item><title>[转载]Windows的多线程程序设计初步</title><link>http://www.cppblog.com/renzhe/articles/1217.html</link><dc:creator>忍者的垃圾堆</dc:creator><author>忍者的垃圾堆</author><pubDate>Mon, 21 Nov 2005 06:41:00 GMT</pubDate><guid>http://www.cppblog.com/renzhe/articles/1217.html</guid><wfw:comment>http://www.cppblog.com/renzhe/comments/1217.html</wfw:comment><comments>http://www.cppblog.com/renzhe/articles/1217.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/renzhe/comments/commentRss/1217.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/renzhe/services/trackbacks/1217.html</trackback:ping><description><![CDATA[一般情况下多线程编程多采用MFC类库实现，那么如果不使用MFC 如何进行多线程程序设计呢？本文将就这个问题进行讨论： 
<P>　　微软在Windows API中提供了建立新的线程的函数CreateThread，它的语法如下：</P>
<P>hThread = CreateThread (&amp;security_attributes, dwStackSize, ThreadProc,pParam, dwFlags, &amp;idThread) ; </P>
<P>　　第一个参数是指向SECURITY_ATTRIBUTES型态的结构的指针。在Windows 98中忽略该参数。在Windows NT中，它被设为NULL。第二个参数是用于新线程的初始堆栈大小，默认值为0。在任何情况下，Windows根据需要动态延长堆栈的大小。</P>
<P>　　CreateThread的第三个参数是指向线程函数的指标。函数名称没有限制，但是必须以下列形式声明：</P>
<P>DWORD WINAPI ThreadProc (PVOID pParam) ; <BR>　 <BR>　　CreateThread的第四个参数为传递给ThreadProc的参数。这样主线程和从属线程就可以共享数据。</P>
<P>　　CreateThread的第五个参数通常为0，但当建立的线程不马上执行时为旗标CREATE_SUSPENDED。线程将暂停直到呼叫ResumeThread来恢复线程的执行为止。第六个参数是一个指标，指向接受执行绪ID值的变量。</P>
<P>　　大多数Windows程序写作者喜欢用在PROCESS.H表头文件中声明的C执行时期链接库函数_beginthread。它的语法如下：</P>
<P>hThread = _beginthread (ThreadProc, uiStackSize, pParam) ; </P>
<P>　　它更简单，对于大多数应用程序很完美，这个线程函数的语法为：</P>
<P>void __cdecl ThreadProc (void * pParam) ; </P>
<P>　　在建立多线程的Windows程序时，需要在「Project Settings」对话框中做一些修改。选择「C/C++」页面标签，然后在「Category」下拉式清单方块中选择「Code Generation」。在「Use Run-Time Library」下拉式清单方块中，可以看到用于「Release」设定的「Single-Threaded」和用于Debug设定的「Debug Single-Threaded」。将这些分别改为「Multithreaded」和「Debug Multithreaded」。这将把编译器旗标改为/MT，它是编译器在编译多线程的应用程序所需要的。</P>
<P>　　第一个demo.</P>
<P>/*******************************************************<BR>*<BR>* deom1---四个线程同时写一个文件( 没有参数 ) <BR>*<BR>*<BR>***********************************************************/<BR>＃i nclude &lt;windows.h&gt;<BR>＃i nclude &lt;process.h&gt; /* _beginthread, _endthread */<BR>＃i nclude &lt;iostream&gt;<BR>＃i nclude &lt;fstream&gt;<BR>using namespace std;</P>
<P>ofstream out("out.txt");</P>
<P>void ThreadFunc1(PVOID param)<BR>{ <BR>　while(1)<BR>　{<BR>　　Sleep(10);<BR>　　out&lt;&lt;"This was draw by thread l"&lt;&lt;endl;<BR>　}<BR>}<BR>void ThreadFunc2(PVOID param)<BR>{ <BR>　while(1)<BR>　{<BR>　　Sleep(10);<BR>　　out&lt;&lt;"This was draw by thread 2"&lt;&lt;endl;<BR>　}<BR>}<BR>void ThreadFunc3(PVOID param)<BR>{ <BR>　while(1)<BR>　{<BR>　　Sleep(10);<BR>　　out&lt;&lt;"This was draw by thread 3"&lt;&lt;endl;<BR>　}<BR>}<BR>void ThreadFunc4(PVOID param)<BR>{ <BR>　while(1)<BR>　{<BR>　　Sleep(10);<BR>　　out&lt;&lt;"This was draw by thread 4"&lt;&lt;endl;<BR>　}<BR>}<BR>int main()<BR>{<BR>　int i=0;<BR>　_beginthread(ThreadFunc1,0,NULL);<BR>　_beginthread(ThreadFunc2,0,NULL);</P>
<P>　_beginthread(ThreadFunc3,0,NULL);<BR>　_beginthread(ThreadFunc4,0,NULL);<BR>　Sleep(3000);<BR>　out&lt;&lt;"end";<BR>　return 0;<BR>}</P>
<P>//demo1 end-----------------------------------------------</P>
<P>第二个demo.<BR>/*******************************************************<BR>*<BR>* deom2---四个线程同时写一个文件( 有参数 ) <BR>*<BR>*<BR>***********************************************************/<BR>＃i nclude &lt;windows.h&gt;<BR>＃i nclude &lt;process.h&gt; /* _beginthread, _endthread */<BR>＃i nclude &lt;iostream&gt;<BR>＃i nclude &lt;fstream&gt;<BR>＃i nclude &lt;string&gt;<BR>using namespace std;<BR>ofstream out("out.txt");</P>
<P>void ThreadFunc1(PVOID param)<BR>{ <BR>　while(1)<BR>　{<BR>　　char *p;<BR>　　p=(char *) param;<BR>　　Sleep(10);<BR>　　out&lt;&lt;p&lt;&lt;"This was draw by thread l"&lt;&lt;endl;<BR>　}<BR>}<BR>void ThreadFunc2(PVOID param)<BR>{ <BR>　while(1)<BR>　{<BR>　　Sleep(10);<BR>　　out&lt;&lt;"This was draw by thread 2"&lt;&lt;endl;<BR>　}<BR>}<BR>void ThreadFunc3(PVOID param)<BR>{ <BR>　while(1)<BR>　{<BR>　　Sleep(10);<BR>　　out&lt;&lt;"This was draw by thread 3"&lt;&lt;endl;<BR>　}<BR>}<BR>void ThreadFunc4(PVOID param)<BR>{ <BR>　while(1)<BR>　{<BR>　　Sleep(10);<BR>　　out&lt;&lt;"This was draw by thread 4"&lt;&lt;endl;<BR>　}<BR>}<BR>int main()<BR>{<BR>　char *pstr=" 参数传递成功";</P>
<P>　_beginthread(ThreadFunc1,0,pstr);<BR>　_beginthread(ThreadFunc2,0,NULL);</P>
<P>　_beginthread(ThreadFunc3,0,NULL);<BR>　_beginthread(ThreadFunc4,0,NULL);<BR>　Sleep(1000);<BR>　out&lt;&lt;"end";<BR>　return 0;<BR>}</P>
<P>// demo2 end ------------------------------------------------</P>
<P>第三个demo( 一个win32 应用程序 ) </P>
<P>/*******************************************************<BR>*<BR>* deom3--- 在屏幕上随机画出一系列矩形<BR>*<BR>*<BR>***********************************************************/</P>
<P><BR>＃i nclude &lt;windows.h&gt;<BR>＃i nclude &lt;process.h&gt;</P>
<P>LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;<BR>HWND hwnd ;</P>
<P>int cxClient, cyClient ;</P>
<P>int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,</P>
<P>PSTR szCmdLine, int iCmdShow)<BR>{<BR>　static TCHAR szAppName[] = TEXT ("RndRctMT") ;<BR>　 MSG msg ;</P>
<P>WNDCLASS wndclass ;</P>
<P>wndclass.style = CS_HREDRAW | CS_VREDRAW ;<BR>wndclass.lpfnWndProc = WndProc ;<BR>wndclass.cbClsExtra = 0 ;<BR>wndclass.cbWndExtra = 0 ;<BR>wndclass.hInstance = hInstance ;<BR>wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;<BR>wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;<BR>wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;<BR>wndclass.lpszMenuName = NULL ;<BR>wndclass.lpszClassName = szAppName ;</P>
<P>if (!RegisterClass (&amp;wndclass))<BR>{<BR>　MessageBox (NULL, TEXT ("This program requires Windows NT!"),szAppName, MB_ICONERROR) ;<BR>　return 0 ;<BR>}</P>
<P>hwnd = CreateWindow ( szAppName, TEXT ("Random Rectangles"),<BR>　　WS_OVERLAPPEDWINDOW,<BR>　　CW_USEDEFAULT, CW_USEDEFAULT,<BR>　　CW_USEDEFAULT, CW_USEDEFAULT,<BR>　　NULL, NULL, hInstance, NULL) ;<BR>ShowWindow (hwnd, iCmdShow) ;<BR>UpdateWindow (hwnd) ;</P>
<P>while (GetMessage (&amp;msg, NULL, 0, 0))<BR>{<BR>　TranslateMessage (&amp;msg) ;<BR>　DispatchMessage (&amp;msg) ;<BR>}<BR>return msg.wParam ;<BR>}</P>
<P>VOID Thread (PVOID pvoid)<BR>{<BR>　HBRUSH hBrush ;<BR>　HDC hdc ;<BR>　int xLeft, xRight, yTop, yBottom, iRed, iGreen, iBlue ;</P>
<P>　while (TRUE)<BR>　{<BR>　　if (cxClient != 0 || cyClient != 0)<BR>　　{<BR>　　　xLeft = rand () % cxClient ;<BR>　　　xRight = rand () % cxClient ;<BR>　　　yTop = rand () % cyClient ;<BR>　　　yBottom = rand () % cyClient ;<BR>　　　iRed = rand () &amp; 255 ;<BR>　　　iGreen = rand () &amp; 255 ;<BR>　　　iBlue = rand () &amp; 255 ;<BR>　 <BR>　　　hdc = GetDC (hwnd) ;<BR>　　　hBrush = CreateSolidBrush (RGB (iRed, iGreen, iBlue)) ;<BR>　　　SelectObject (hdc, hBrush) ;</P>
<P>　　　Rectangle (hdc,min (xLeft, xRight), min (yTop, yBottom),<BR>　　　　　max (xLeft, xRight), max (yTop, yBottom)) ;</P>
<P>　　　ReleaseDC (hwnd, hdc) ;<BR>　　　DeleteObject (hBrush) ;<BR>　　}<BR>　}<BR>}</P>
<P>LRESULT CALLBACK WndProc ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<BR>{<BR>　switch (message)<BR>　{<BR>　　case WM_CREATE:<BR>　　　_beginthread (Thread, 0, NULL) ;<BR>　　　return 0 ;<BR>　　case WM_SIZE:<BR>　　　cxClient = LOWORD (lParam) ;<BR>　　　cyClient = HIWORD (lParam) ;<BR>　　　return 0 ;<BR>　　case WM_DESTROY:<BR>　　　PostQuitMessage (0) ;<BR>　　　return 0 ;<BR>　}<BR>　return DefWindowProc (hwnd, message, wParam, lParam) ;<BR>}</P>
<P>//demo4 end-----------------------------------------------</P><img src ="http://www.cppblog.com/renzhe/aggbug/1217.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/renzhe/" target="_blank">忍者的垃圾堆</a> 2005-11-21 14:41 <a href="http://www.cppblog.com/renzhe/articles/1217.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>