⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_32524.htm

📁 一些关于vc的问答
💻 HTM
字号:
<p>
序号:32524 发表者:lin min 发表日期:2003-03-12 19:07:33
<br>主题:msdn里的例子出了编译错误?
<br>内容:/* BEGTHRD.C illustrates multiple threads using functions:<BR> *<BR> *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_beginthread&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_endthread<BR> *<BR> *<BR> * This program requires the multithreaded library. For example,<BR> * compile with the following command line:<BR> *&nbsp;&nbsp;&nbsp;&nbsp; CL /MT /D "_X86_" BEGTHRD.C<BR> *<BR> * If you are using the Visual C++ development environment, select the <BR> * Multi-Threaded runtime library in the compiler Project Settings <BR> * dialog box.<BR> * <BR> */<BR><BR>#include &lt;windows.h&gt;<BR>#include &lt;process.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;/* _beginthread, _endthread */<BR>#include &lt;stddef.h&gt;<BR>#include &lt;stdlib.h&gt;<BR>#include &lt;conio.h&gt;<BR><BR>void Bounce( void *ch );<BR>void CheckKey( void *dummy );<BR><BR>/* GetRandom returns a random integer between min and max. */<BR>#define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))<BR><BR>BOOL repeat = TRUE;&nbsp;&nbsp;&nbsp;&nbsp; /* Global repeat flag and video variable */<BR>HANDLE hStdOut;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Handle for console window */<BR>CONSOLE_SCREEN_BUFFER_INFO csbi;&nbsp;&nbsp;&nbsp;&nbsp;/* Console information structure */<BR><BR>void main()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;CHAR&nbsp;&nbsp;&nbsp;&nbsp;ch = 'A';<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;/* Get display screen's text row and column information. */<BR>&nbsp;&nbsp; GetConsoleScreenBufferInfo( hStdOut, &amp;csbi );<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;/* Launch CheckKey thread to check for terminating keystroke. */<BR>&nbsp;&nbsp;&nbsp;&nbsp;_beginthread( CheckKey, 0, NULL );<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;/* Loop until CheckKey terminates program. */<BR>&nbsp;&nbsp;&nbsp;&nbsp;while( repeat )<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* On first loops, launch character threads. */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_beginthread( Bounce, 0, (void *) (ch++)&nbsp;&nbsp;);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Wait one second between loops. */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sleep( 1000L );<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>}<BR><BR>/* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */<BR>void CheckKey( void *dummy )<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;_getch();<BR>&nbsp;&nbsp;&nbsp;&nbsp;repeat = 0;&nbsp;&nbsp;&nbsp;&nbsp;/* _endthread implied */<BR><BR>}<BR><BR>/* Bounce - Thread to create and and control a colored letter that moves<BR> * around on the screen.<BR> *<BR> * Params: ch - the letter to be moved<BR> */<BR>void Bounce( void *ch )<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;/* Generate letter and color attribute from thread argument. */<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;&nbsp;blankcell = 0x20;<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;&nbsp;blockcell = (char) ch;<BR>&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;&nbsp;&nbsp;&nbsp;first = TRUE;<BR>&nbsp;&nbsp; COORD&nbsp;&nbsp; oldcoord, newcoord;<BR>&nbsp;&nbsp; DWORD&nbsp;&nbsp; result;<BR><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;/* Seed random number generator and get initial location. */<BR>&nbsp;&nbsp;&nbsp;&nbsp;srand( _threadid );<BR>&nbsp;&nbsp;&nbsp;&nbsp;newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 );<BR>&nbsp;&nbsp;&nbsp;&nbsp;newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 );<BR>&nbsp;&nbsp;&nbsp;&nbsp;while( repeat )<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Pause between loops. */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sleep( 100L );<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Blank out our old position on the screen, and draw new letter. */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( first )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;first = FALSE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WriteConsoleOutputCharacter( hStdOut, &amp;blankcell, 1, oldcoord, &amp;result );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WriteConsoleOutputCharacter( hStdOut, &amp;blockcell, 1, newcoord, &amp;result );<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Increment the coordinate for next placement of the block. */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oldcoord.X = newcoord.X;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oldcoord.Y = newcoord.Y;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newcoord.X += GetRandom( -1, 1 );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newcoord.Y += GetRandom( -1, 1 );<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Correct placement (and beep) if about to go off the screen. */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( newcoord.X &lt; 0 )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newcoord.X = 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if( newcoord.X == csbi.dwSize.X )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newcoord.X = csbi.dwSize.X - 2;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if( newcoord.Y &lt; 0 )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newcoord.Y = 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if( newcoord.Y == csbi.dwSize.Y )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newcoord.Y = csbi.dwSize.Y - 2;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* If not at a screen border, continue, otherwise beep. */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Beep( ((char) ch - 'A') * 100, 175 );<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;/* _endthread given to terminate */<BR>&nbsp;&nbsp;&nbsp;&nbsp;_endthread();<BR>}<BR><BR>error C2065: '_beginthread' : undeclared identifier<BR>error C2065: '_threadid' : undeclared identifier<BR>error C2065: '_endthread' : undeclared identifier<BR><BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:jerrer 回复日期:2003-03-12 20:22:40
<br>内容:将Project setting的c/c++中的code generation选项中的Use-runtime library改为<BR>Debug Multithreaded即可。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -