📄 subject_32524.htm
字号:
<p>
序号:32524 发表者:lin min 发表日期:2003-03-12 19:07:33
<br>主题:msdn里的例子出了编译错误?
<br>内容:/* BEGTHRD.C illustrates multiple threads using functions:<BR> *<BR> * _beginthread _endthread<BR> *<BR> *<BR> * This program requires the multithreaded library. For example,<BR> * compile with the following command line:<BR> * 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 <windows.h><BR>#include <process.h> /* _beginthread, _endthread */<BR>#include <stddef.h><BR>#include <stdlib.h><BR>#include <conio.h><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; /* Global repeat flag and video variable */<BR>HANDLE hStdOut; /* Handle for console window */<BR>CONSOLE_SCREEN_BUFFER_INFO csbi; /* Console information structure */<BR><BR>void main()<BR>{<BR> CHAR ch = 'A';<BR><BR> hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );<BR><BR> /* Get display screen's text row and column information. */<BR> GetConsoleScreenBufferInfo( hStdOut, &csbi );<BR><BR> /* Launch CheckKey thread to check for terminating keystroke. */<BR> _beginthread( CheckKey, 0, NULL );<BR><BR> /* Loop until CheckKey terminates program. */<BR> while( repeat )<BR> {<BR> /* On first loops, launch character threads. */<BR> _beginthread( Bounce, 0, (void *) (ch++) );<BR><BR> /* Wait one second between loops. */<BR> Sleep( 1000L );<BR> }<BR>}<BR><BR>/* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */<BR>void CheckKey( void *dummy )<BR>{<BR> _getch();<BR> repeat = 0; /* _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> /* Generate letter and color attribute from thread argument. */<BR> char blankcell = 0x20;<BR> char blockcell = (char) ch;<BR> BOOL first = TRUE;<BR> COORD oldcoord, newcoord;<BR> DWORD result;<BR><BR><BR> /* Seed random number generator and get initial location. */<BR> srand( _threadid );<BR> newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 );<BR> newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 );<BR> while( repeat )<BR> {<BR> /* Pause between loops. */<BR> Sleep( 100L );<BR><BR> /* Blank out our old position on the screen, and draw new letter. */<BR> if( first )<BR> first = FALSE;<BR> else<BR> WriteConsoleOutputCharacter( hStdOut, &blankcell, 1, oldcoord, &result );<BR> WriteConsoleOutputCharacter( hStdOut, &blockcell, 1, newcoord, &result );<BR><BR> /* Increment the coordinate for next placement of the block. */<BR> oldcoord.X = newcoord.X;<BR> oldcoord.Y = newcoord.Y;<BR> newcoord.X += GetRandom( -1, 1 );<BR> newcoord.Y += GetRandom( -1, 1 );<BR><BR> /* Correct placement (and beep) if about to go off the screen. */<BR> if( newcoord.X < 0 )<BR> newcoord.X = 1;<BR> else if( newcoord.X == csbi.dwSize.X )<BR> newcoord.X = csbi.dwSize.X - 2;<BR> else if( newcoord.Y < 0 )<BR> newcoord.Y = 1;<BR> else if( newcoord.Y == csbi.dwSize.Y )<BR> newcoord.Y = csbi.dwSize.Y - 2;<BR><BR> /* If not at a screen border, continue, otherwise beep. */<BR> else<BR> continue;<BR> Beep( ((char) ch - 'A') * 100, 175 );<BR> }<BR> /* _endthread given to terminate */<BR> _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 + -