beginthread

beginthread

_beginthread,VC/VC++函數,創建一個線程

頭文件


介紹


函數創建一個線程。
uintptr_t _beginthread(
void( *start_address )( void * ),
unsigned stack_size,
void *arglist
);
返回值:
假如成功,函數將會返回一個新線程的句柄,用戶可以像這樣聲明一個句柄變數存儲返回值:
HANDLE hStdOut = _beginthread( CheckKey, 0, NULL )。如果失敗_beginthread將返回-1。
start_address
新線程的起始地址,指向新線程調用的函數的起始地址
stack_size
新線程的堆棧大小,可以為0
arglist
傳遞給線程的參數列表,無參數時為NULL
所在庫文件:
#include

示例:


// crt_BEGTHRD.C
// compile with: /MT /D "_X86_" /c
// processor: x86
#include
#include
#include
#include
#include <conio.h>
void Bounce( void *ch );
void CheckKey( void *dummy );
#define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))
BOOL repeat = TRUE;
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
int main()
{
CHAR ch = 'A';
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
GetConsoleScreenBufferInfo( hStdOut, &csbi );
_beginthread( CheckKey, 0, NULL );
while( repeat )
{
_beginthread( Bounce, 0, (void *) (ch++) );
Sleep( 1000L );
}
}
void CheckKey( void *dummy )
{
_getch();
repeat = 0;
}
void Bounce( void *ch )
{
char blankcell = 0x20;
char blockcell = (char) ch;
BOOL first = TRUE;
COORD oldcoord, newcoord;
DWORD result;
srand( _threadid );
newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 );
newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 );
while( repeat )
{
Sleep( 100L );
if( first )
first = FALSE;
else
WriteConsoleOutputCharacter( hStdOut, &blankcell, 1, oldcoord, &result );
WriteConsoleOutputCharacter( hStdOut, &blockcell, 1, newcoord, &result );
oldcoord.X = newcoord.X;
oldcoord.Y = newcoord.Y;
newcoord.X += GetRandom( -1, 1 );
newcoord.Y += GetRandom( -1, 1 );
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;
else
continue;
Beep( ((char) ch - 'A') * 100, 175 );
}
_endthread();
}