Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1572207
  • 博文数量: 596
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 173
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-06 15:50
个人简介

在线笔记

文章分类

全部博文(596)

文章存档

2016年(1)

2015年(104)

2014年(228)

2013年(226)

2012年(26)

2011年(11)

分类: Windows平台

2014-06-20 17:46:03

(v=vs.71).aspx
  1. Create a thread.
  2. uintptr_t _beginthread(
  3.    void( __cdecl *start_address )( void * ),
  4.    unsigned stack_size,
  5.    void *arglist
  6. );
  7. uintptr_t _beginthreadex(
  8.    void *security,
  9.    unsigned stack_size,
  10.    unsigned ( __stdcall *start_address )( void * ),
  11.    void *arglist,
  12.    unsigned initflag,
  13.    unsigned *thrdaddr
  14. );

  15. Parameters

  16. start_address
  17. Start address of routine that begins execution of new thread.
  18. stack_size
  19. Stack size for new thread or 0.
  20. arglist
  21. Argument list to be passed to new thread or NULL.
  22. security
  23. Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If NULL, the handle cannot be inherited. Must be NULL for Windows 95 applications.
  24. initflag
  25. Initial state of new thread (0 for running or CREATE_SUSPENDED for suspended); use ResumeThread to execute the thread.
  26. thrdaddr
  27. Points to a 32-bit variable that receives the thread identifier.

  28. Return Value
  29. If successful, each of these functions returns a handle to the newly created thread. _beginthread returns –1L on an error, in which case errno is set to EAGAIN if there are too many threads, or to EINVAL if the argument is invalid or the stack size is incorrect. _beginthreadex returns 0 on an error, in which case errno and _doserrno are set.(成功的话,返回对应的线程句柄。_beginthread失败返回-1,EAGAIN表示当前线程太多而无法创建,EINVAL表示参数错误。_beginthreadex 失败返回0)

  30. See Standard Types for more information on uintptr_t.
  31. See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these and other return codes.
  32. Remarks
  33. The _beginthread function creates a thread that begins execution of a routine at start_address. The routine at start_address must use the __cdecl calling convention and should have no return value. When the thread returns from that routine, it is terminated automatically. For more information on threads, see Multithreading.
  34. _beginthreadex resembles the Win32 CreateThread API more closely than _beginthread does. _beginthreadex differs from _beginthread in the following ways:
  35. _beginthreadex has three additional parameters: initflag, security, threadaddr. The new thread can be created in a suspended state, with a specified security (Windows NT only), and can be accessed using thrdaddr, which is the thread identifier.
  36. The routine at start_address passed to _beginthreadex must use the __stdcall calling convention and must return a thread exit code.
  37. _beginthreadex returns 0 on failure, rather than –1L.
  38. A thread created with _beginthreadex is terminated by a call to _endthreadex.
  39. The _beginthreadex function gives you more control over how the thread is created than _beginthread does. The _endthreadex function is also more flexible. For example, with _beginthreadex, you can use security information, set the initial state of the thread (running or suspended), and get the thread identifier of the newly created thread. You are also able to use the thread handle returned by _beginthreadex with the synchronization APIs, which you cannot do with _beginthread.
  40. It is safer to use _beginthreadex than _beginthread. If the thread spawned by _beginthread exits quickly, the handle returned to the caller of _beginthread may be invalid or, worse, point to another thread. However, the handle returned by _beginthreadex has to be closed by the caller of _beginthreadex, so it is guaranteed to be a valid handle if _beginthreadex did not return an error.
  41. You can call _endthread or _endthreadex explicitly to terminate a thread; however, _endthread or _endthreadex is called automatically when the thread returns from the routine passed as a parameter. Terminating a thread with a call to endthread or _endthreadex helps to ensure proper recovery of resources allocated for the thread.
  42. _endthread automatically closes the thread handle (whereas _endthreadex does not). Therefore, when using _beginthread and _endthread, do not explicitly close the thread handle by calling the Win32 CloseHandle API. This behavior differs from the Win32 ExitThread API.
  43. Note For an executable file linked with LIBCMT.LIB, do not call the Win32 ExitThread API; this prevents the run-time system from reclaiming allocated resources. _endthread and _endthreadex reclaim allocated thread resources and then call ExitThread.
  44. The operating system handles the allocation of the stack when either _beginthread or _beginthreadex is called; you do not need to pass the address of the thread stack to either of these functions. In addition, the stack_size argument can be 0, in which case the operating system uses the same value as the stack specified for the main thread.
  45. arglist is a parameter to be passed to the newly created thread. Typically it is the address of a data item, such as a character string. arglist may be NULL if it is not needed, but _beginthread and _beginthreadex must be provided with some value to pass to the new thread. All threads are terminated if any thread calls abort, exit, _exit, or ExitProcess.
  46. Requirements
  47. Routine    Required header    Compatibility
  48. _beginthread    <process.h>    Win 98, Win Me, Win NT, Win 2000, Win XP
  49. _beginthreadex    <process.h>    Win 98, Win Me, Win NT, Win 2000, Win XP
  50. For additional compatibility information, see Compatibility in the Introduction.
  51. Libraries
  52. Multithreaded versions of the C run-time libraries only.
  53. To use _beginthread or _beginthreadex, the application must link with one of the multithreaded C run-time libraries.
  54. Example
  55. The following example uses _beginthread and _endthread.
  56. // crt_BEGTHRD.C
  57. // compile with: /MT /D "_X86_" /c
  58. #include <windows.h>
  59. #include <process.h> /* _beginthread, _endthread */
  60. #include <stddef.h>
  61. #include <stdlib.h>
  62. #include <conio.h>

  63. void Bounce( void *ch );
  64. void CheckKey( void *dummy );

  65. /* GetRandom returns a random integer between min and max. */
  66. #define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))

  67. BOOL repeat = TRUE; /* Global repeat flag and video variable */
  68. HANDLE hStdOut; /* Handle for console window */
  69. CONSOLE_SCREEN_BUFFER_INFO csbi; /* Console information structure */

  70. int main()
  71. {
  72.     CHAR ch = 'A';

  73.     hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );

  74.     /* Get display screen's text row and column information. */
  75.    GetConsoleScreenBufferInfo( hStdOut, &csbi );

  76.     /* Launch CheckKey thread to check for terminating keystroke. */
  77.     _beginthread( CheckKey, 0, NULL );

  78.     /* Loop until CheckKey terminates program. */
  79.     while( repeat )
  80.     {
  81.         /* On first loops, launch character threads. */
  82.         _beginthread( Bounce, 0, (void *) (ch++) );

  83.         /* Wait one second between loops. */
  84.         Sleep( 1000L );
  85.     }
  86. }

  87. /* CheckKey - Thread to wait for a keystroke, then clear repeat flag. */
  88. void CheckKey( void *dummy )
  89. {
  90.     _getch();
  91.     repeat = 0; /* _endthread implied */

  92. }

  93. /* Bounce - Thread to create and and control a colored letter that moves
  94.  * around on the screen.
  95.  *
  96.  * Params: ch - the letter to be moved
  97.  */
  98. void Bounce( void *ch )
  99. {
  100.     /* Generate letter and color attribute from thread argument. */
  101.     char blankcell = 0x20;
  102.     char blockcell = (char) ch;
  103.     BOOL first = TRUE;
  104.    COORD oldcoord, newcoord;
  105.    DWORD result;


  106.     /* Seed random number generator and get initial location. */
  107.     srand( _threadid );
  108.     newcoord.X = GetRandom( 0, csbi.dwSize.X - 1 );
  109.     newcoord.Y = GetRandom( 0, csbi.dwSize.Y - 1 );
  110.     while( repeat )
  111.     {
  112.         /* Pause between loops. */
  113.         Sleep( 100L );

  114.         /* Blank out our old position on the screen, and draw new letter. */
  115.         if( first )
  116.             first = FALSE;
  117.         else
  118.          WriteConsoleOutputCharacter( hStdOut, &blankcell, 1, oldcoord, &result );
  119.          WriteConsoleOutputCharacter( hStdOut, &blockcell, 1, newcoord, &result );

  120.         /* Increment the coordinate for next placement of the block. */
  121.         oldcoord.X = newcoord.X;
  122.         oldcoord.Y = newcoord.Y;
  123.         newcoord.X += GetRandom( -1, 1 );
  124.         newcoord.Y += GetRandom( -1, 1 );

  125.         /* Correct placement (and beep) if about to go off the screen. */
  126.         if( newcoord.X < 0 )
  127.             newcoord.X = 1;
  128.         else if( newcoord.X == csbi.dwSize.X )
  129.             newcoord.X = csbi.dwSize.X - 2;
  130.         else if( newcoord.Y < 0 )
  131.             newcoord.Y = 1;
  132.         else if( newcoord.Y == csbi.dwSize.Y )
  133.             newcoord.Y = csbi.dwSize.Y - 2;

  134.         /* If not at a screen border, continue, otherwise beep. */
  135.         else
  136.             continue;
  137.         Beep( ((char) ch - 'A

阅读(1153) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~