加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

_beginthread函数解释及实例应用

(2017-05-13 16:36:04)
标签:

it

编程

程序

分类: 编程语言
_beginthread函数解释及实例应用

_beginthread
uintptr_t _beginthread( 
   void( *start_address )( void * ),
   unsigned stack_size,
   void *arglist 
);


Parameters 参数:
start_address:程序执行一个新线程的起始地址
Start address of a routine that begins execution of a new thread. For _beginthread, the calling convention is either __cdecl or __clrcall; for _beginthreadex, it is either __stdcall or __clrcall.

stack_size:新线程的堆栈大小或0
Stack size for a new thread or 0.

Arglist:传给新线程的变量清单或空
Argument list to be passed to a new thread or NULL.


Return Value 返回值:
如果新线程建立成功,函数返回该线程的句柄;然而,如果新线程退出太快,_beginthread函数可能返回一个有误的句柄。_beginthread发生错误时返回1L。
If successful, function returns a handle to the newly created thread; however, if the newly created thread exits too quickly, _beginthread might not return a valid handle. _beginthread returns 1L on an error, in which case errno is set to EAGAIN if there are too many threads, to EINVAL if the argument is invalid or the stack size is incorrect, or to EACCES in the case of insufficient resources (such as memory). 


Requirements 要求:
Routine(程序)
Required header(需要的头文件)
_beginthread



Example 举例:

下面举例教您使用_beginthread

// crt_BEGTHRD.C
// 编译: /MT /D "_X86_" /c
// 处理器: x86
#include
#include   
#include
#include
#include
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();
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有