题目 : 创建线程
1. 目的与要求: 练习使用Windows API的接口函数创建两个线程,使其完成不同的功能,如一个用于输出字符‘A’、一个输出字符‘B’。
2. 任选一种编程语言实现。
刚学完操作系统,这是我们的一个实验题目。请大家说一下实现他的基本思想和主要用那几个API函数,谢谢
1. 目的与要求: 练习使用Windows API的接口函数创建两个线程,使其完成不同的功能,如一个用于输出字符‘A’、一个输出字符‘B’。
2. 任选一种编程语言实现。
刚学完操作系统,这是我们的一个实验题目。请大家说一下实现他的基本思想和主要用那几个API函数,谢谢
解决方案 »
- 有人用过 tidy 这个开源的库吗?
- vc2008中如何创建Win32 Dynamic-Link Library工程
- 那位大哥大姐会用C++语言实现一个函数的傅立叶变换啊?毕业设计要用 啊////
- 种子填充算法的非递归算法怎么写啊
- 如何打印一个对话框?
- 我用CStatic加载了一个HBITMAP,我怎么能得到这个BITMAP在当前DC下的大小尺寸啊,我主要想知道长和宽,要换成DC值,谢谢大家!
- 一个关于memcpy的问题
- 各位怎么样让程序一开始运行就隐藏呀!
- 有c基础,vc学习一个月,要用vc做毕业设计
- 用vc编邮件服务程序,如何入手
- 为什么出错?ADO访问数据库问题!
- 急,不装Visual Studior如何打开Access数据库?
你的要求很简单,不需要其它函数了。
线程在函数结束时自动终结。
线程必须声明为以下形式:
UINT MyControllingFunction( LPVOID pParam );
*
* _beginthread _endthread
*
*
* This program requires the multithreaded library. For example,
* compile with the following command line:
* CL /MT /D "_X86_" BEGTHRD.C
*
* If you are using the Visual C++ development environment, select the
* Multi-Threaded runtime library in the compiler Project Settings
* dialog box.
*
*/#include <windows.h>
#include <process.h> /* _beginthread, _endthread */
#include <stddef.h>
#include <stdlib.h>
#include <conio.h>void Bounce( void *ch );
void CheckKey( void *dummy );/* GetRandom returns a random integer between min and max. */
#define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))BOOL repeat = TRUE; /* Global repeat flag and video variable */
HANDLE hStdOut; /* Handle for console window */
CONSOLE_SCREEN_BUFFER_INFO csbi; /* Console information structure */void main()
{
CHAR ch = 'A'; hStdOut = GetStdHandle( STD_OUTPUT_HANDLE ); /* Get display screen's text row and column information. */
GetConsoleScreenBufferInfo( hStdOut, &csbi ); /* Launch CheckKey thread to check for terminating keystroke. */
_beginthread( CheckKey, 0, NULL ); /* Loop until CheckKey terminates program. */
while( repeat )
{
/* On first loops, launch character threads. */
_beginthread( Bounce, 0, (void *) (ch++) ); /* Wait one second between loops. */
Sleep( 1000L );
}
}/* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */
void CheckKey( void *dummy )
{
_getch();
repeat = 0; /* _endthread implied */}/* Bounce - Thread to create and and control a colored letter that moves
* around on the screen.
*
* Params: ch - the letter to be moved
*/
void Bounce( void *ch )
{
/* Generate letter and color attribute from thread argument. */
char blankcell = 0x20;
char blockcell = (char) ch;
BOOL first = TRUE;
COORD oldcoord, newcoord;
DWORD result;
/* Seed random number generator and get initial location. */
srand( _threadid );
newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 );
newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 );
while( repeat )
{
/* Pause between loops. */
Sleep( 100L ); /* Blank out our old position on the screen, and draw new letter. */
if( first )
first = FALSE;
else
WriteConsoleOutputCharacter( hStdOut, &blankcell, 1, oldcoord, &result );
WriteConsoleOutputCharacter( hStdOut, &blockcell, 1, newcoord, &result ); /* Increment the coordinate for next placement of the block. */
oldcoord.X = newcoord.X;
oldcoord.Y = newcoord.Y;
newcoord.X += GetRandom( -1, 1 );
newcoord.Y += GetRandom( -1, 1 ); /* Correct placement (and beep) if about to go off the screen. */
if( newcoord.X < 0 )
newcoord.X = 1;
else if( newcoord.X == csbi.dwSize.X )
newcoord.X = csbi.dwSize.X - 2;
else if( newcoord.Y < 0 )
newcoord.Y = 1;
else if( newcoord.Y == csbi.dwSize.Y )
newcoord.Y = csbi.dwSize.Y - 2; /* If not at a screen border, continue, otherwise beep. */
else
continue;
Beep( ((char) ch - 'A') * 100, 175 );
}
/* _endthread given to terminate */
_endthread();
}