实验函数文档vc++1.doc_第1页
实验函数文档vc++1.doc_第2页
实验函数文档vc++1.doc_第3页
实验函数文档vc++1.doc_第4页
实验函数文档vc++1.doc_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

头文件: #include 库文件:kernel32.libCreateThreadThe CreateThread function creates a thread to execute within the address space of the calling process. HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, / pointer to security attributes DWORD dwStackSize, / initial thread stack size LPTHREAD_START_ROUTINE lpStartAddress, / pointer to thread function LPVOID lpParameter, / argument for new thread DWORD dwCreationFlags, / creation flags LPDWORD lpThreadId / pointer to receive thread ID); ParameterslpThreadAttributes Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpThreadAttributes is NULL, the handle cannot be inherited. Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new thread. If lpThreadAttributes is NULL, the thread gets a default security descriptor. dwStackSize Specifies the initial commit size of the stack, in bytes. The system rounds this value to the nearest page. If this value is zero, or is smaller than the default commit size, the default is to use the same size as the calling thread. For more information, see Thread Stack Size. lpStartAddress Pointer to the application-defined function of type LPTHREAD_START_ROUTINE to be executed by the thread and represents the starting address of the thread. For more information on the thread function, see ThreadProc. lpParameter Specifies a single 32-bit parameter value passed to the thread. dwCreationFlags Specifies additional flags that control the creation of the thread. If the CREATE_SUSPENDED flag is specified, the thread is created in a suspended state, and will not run until the ResumeThread function is called. If this value is zero, the thread runs immediately after creation. At this time, no other values are supported. lpThreadId Pointer to a 32-bit variable that receives the thread identifier. Windows NT: If this parameter is NULL, the thread identifier is not returned. Windows 95 and Windows 98: This parameter may not be NULL. Return ValuesIf the function succeeds, the return value is a handle to the new thread.If the function fails, the return value is NULL. To get extended error information, call GetLastError. Windows 95 and Windows 98: CreateThread succeeds only when it is called in the context of a 32-bit program. A 32-bit DLL cannot create an additional thread when that DLL is being called by a 16-bit program.RemarksThe new thread handle is created with THREAD_ALL_ACCESS to the new thread. If a security descriptor is not provided, the handle can be used in any function that requires a thread object handle. When a security descriptor is provided, an access check is performed on all subsequent uses of the handle before access is granted. If the access check denies access, the requesting process cannot use the handle to gain access to the thread. The thread execution begins at the function specified by the lpStartAddress parameter. If this function returns, the DWORD return value is used to terminate the thread in an implicit call to the ExitThread function. Use the GetExitCodeThread function to get the threads return value. The CreateThread function may succeed even if lpStartAddress points to data, code, or is not accessible. If the start address is invalid when the thread runs, an exception occurs, and the thread terminates. Thread termination due to a invalid start address is handled as an error exit for the threads process. This behavior is similar to the asynchronous nature of CreateProcess, where the process is created even if it refers to invalid or missing dynamic-link libraries (DLLs). The thread is created with a thread priority of THREAD_PRIORITY_NORMAL. Use the GetThreadPriority and SetThreadPriority functions to get and set the priority value of a thread. When a thread terminates, the thread object attains a signaled state, satisfying any threads that were waiting on the object. The thread object remains in the system until the thread has terminated and all handles to it have been closed through a call to CloseHandle. The ExitProcess, ExitThread, CreateThread, CreateRemoteThread functions, and a process that is starting (as the result of a call by CreateProcess) are serialized between each other within a process. Only one of these events can happen in an address space at a time. This means that the following restrictions hold: During process startup and DLL initialization routines, new threads can be created, but they do not begin execution until DLL initialization is done for the process. Only one thread in a process can be in a DLL initialization or detach routine at a time. ExitProcess does not return until no threads are in their DLL initialization or detach routines. A thread that uses functions from the C run-time libraries should use the beginthread and endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called. Windows CE: The lpThreadAttributes parameter must be set to NULL. The dwStackSize parameter must be zero. Only zero or CREATE_SUSPENDED values are supported for the dwCreationFlags parameter.QuickInfoWindows NT: Requires version 3.1 or later.Windows: Requires Windows 95 or later.Windows CE: Requires version 1.01 or later.Header: Declared in winbase.h.Import Library: Use kernel32.lib.See AlsoProcesses and Threads Overview, Process and Thread Functions, CloseHandle, CreateProcess, CreateRemoteThread, ExitProcess, ExitThread, GetExitCodeThread, GetThreadPriority, ResumeThread, SetThreadPriority, SECURITY_ATTRIBUTES, ThreadProc CloseHandleThe CloseHandle function closes an open object handle. BOOL CloseHandle( HANDLE hObject / handle to object to close); ParametershObject Handle to an open object. Return ValuesIf the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError. RemarksThe CloseHandle function closes handles to the following objects: Communications device Console input Console screen buffer Event File File mapping Job Mailslot Mutex Named pipe Process Semaphore Socket Thread Token CloseHandle invalidates the specified object handle, decrements the objects handle count, and performs object retention checks. After the last handle to an object is closed, the object is removed from the system. Closing a thread handle does not terminate the associated thread. To remove a thread object, you must terminate the thread, then close all handles to the thread.Use CloseHandle to close handles returned by calls to the CreateFile function. Use FindClose to close handles returned by calls to the FindFirstFile function.Closing an invalid handle raises an exception. This includes closing a handle twice, not checking the return value and closing an invalid handle, and using CloseHandle on a handle returned by FindFirstFile.Windows CE: Windows CE does not support closing handles to the following objects: Console input or output File mapping Named pipe Semaphore Token QuickInfoWindows NT: Requires version 3.1 or later.Windows: Requires Windows 95 or later.Windows CE: Requires version 1.0 or later.Header: Declared in winbase.h.Import Library: Use kernel32.lib.See AlsoHandles and Objects Overview, Handle and Object Functions, CreateFile, DeleteFile, FindClose, FindFirstFileGetCurrentThreadIdThe GetCurrentThreadId function returns the thread identifier of the calling thread. DWORD GetCurrentThreadId(VOID) ParametersThis function has no parameters. Return ValuesThe return value is the thread identifier of the calling thread. RemarksUntil the thread terminates, the thread identifier uniquely identifies the thread throughout the system. QuickInfoWindows NT: Requires version 3.1 or later.Windows: Requires Windows 95 or later.Windows CE: Requires version 1.0 or later.Header: Declared in winbase.h.Import Library: Use kernel32.lib.See AlsoProcesses and Threads Overview, Process and Thread Functions, GetCurrentThread GetCurrentProcessIdThe GetCurrentProcessId function returns the process identifier of the calling process. DWORD GetCurrentProcessId(VOID) ParametersThis function has no parameters. Return ValuesThe return value is the process identifier of the calling process. RemarksUntil the process terminates, the process identifier uniquely identifies the process throughout the system. QuickInfoWindows NT: Requires version 3.1 or later.Windows: Requires Windows 95 or later.Windows CE: Requires version 1.0 or later.Header: Declared in winbase.h.Import Library: Use kernel32.lib.See AlsoProcesses and Threads Overview, Process and Thread Functions, GetCurrentProcess, OpenProcess GetThreadPriorityThe GetThreadPriority function returns the priority value for the specified thread. This value, together with the priority class of the threads process, determines the threads base-priority level. int GetThreadPriority( HANDLE hThread / handle to thread); ParametershThread Handle to the thread. Windows NT: The handle must have THREAD_QUERY_INFORMATION access. Return ValuesIf the function succeeds, the return value is the threads priority level.If the function fails, the return value is THREAD_PRIORITY_ERROR_RETURN. To get extended error information, call GetLastError. The threads priority level is one of the following values: PriorityMeaningTHREAD_PRIORITY_ABOVE_NORMALIndicates 1 point above normal priority for the priority class.THREAD_PRIORITY_BELOW_NORMALIndicates 1 point below normal priority for the priority class.THREAD_PRIORITY_HIGHESTIndicates 2 points above normal priority for the priority class.THREAD_PRIORITY_IDLEIndicates a base-priority level of 1 for IDLE_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS, or HIGH_PRIORITY_CLASS processes, and a base-priority level of 16 for REALTIME_PRIORITY_CLASS processes.THREAD_PRIORITY_LOWESTIndicates 2 points below normal priority for the priority class.THREAD_PRIORITY_NORMALIndicates normal priority for the priority class.THREAD_PRIORITY_TIME_CRITICALIndicates a base-priority level of 15 for IDLE_PRIORITY_CLASS, NORMAL_PRIORITY_CLASS, or HIGH_PRIORITY_CLASS processes, and a base-priority level of 31 for REALTIME_PRIORITY_CLASS processes.RemarksEvery thread has a base-priority level determined by the threads priority value and the priority class of its process. The operating system uses the base-priority level of all executable threads to determine which thread gets the next slice of CPU time. Threads are scheduled in a round-robin fashion at each priority level, and only when there are no executable threads at a higher level will scheduling of threads at a lower level take place. For a table that shows the base-priority levels for each combination of priority class and thread priority value, refer to the SetPriorityClass function. Windows CE: The threads priority level is one of the following values: THREAD_PRIORITY_TIME_CRITICAL Indicates 3 points above normal priority. THREAD_PRIORITY_HIGHEST Indicates 2 points above normal priority. THREAD_PRIORITY_ABOVE_NORMAL Indicates 1 point above normal priority. THREAD_PRIORITY_NORMALIndicates normal priority. THREAD_PRIORITY_BELOW_NORMAL Indicates 1 point below normal priority. THREAD_PRIORITY_LOWESTIndicates 2 points below normal priority. THREAD_PRIORITY_ABOVE_IDLEIndicates 3 points below normal priority. THREAD_PRIORITY_IDLE Indicates 4 points below normal priority. Windows CE does not support priority classes. The order in which threads are scheduled is determined only by their thread priorities.When manipulating priorities, ensure that a high-priority thread does not consume all of the available CPU time. A thread with a priority level of THREAD_PRIORITY_TIME_CRITICAL will execute until it explicitly yields processing to other threads. Processing of these threads is not yielded to other threads with the THREAD_PRIORITY_TIME_CRITICAL priority level. Such a thread can interfere with the normal operation of the operating system if the thread doesnt explicitly yield processing quickly. QuickInfoWindows NT: Requires version 3.1 or later.Windows: Requires Windows 95 or later.Windows CE: Requires version 1.0 or later.Header: Declared in winbase.h.Import Library: Use kernel32.lib.See AlsoProcesses and Threads Overview, Process and Thread Functions, GetPriorityClass, SetPriorityClass, SetThreadPriority CWinThread:SetThreadPriorityBOOL SetThreadPriority( int nPriority );Return ValueNonzero if function was successful; otherwise 0.ParametersnPrioritySpecifies the new thread priority level within its priority class. This parameter must be one of the following values, listed from highest priority to lowest: THREAD_PRIORITY_TIME_CRITICAL THREAD_PRIORITY_HIGHEST THREAD_PRIORITY_ABOVE_NORMAL THREAD_PRIORITY_NORMAL THREAD_PRIORITY_BELOW_NORMAL THREAD_PRIORITY_LOWEST THREAD_PRIORITY_IDLE For more information on these priorities, see :SetThreadPriority in the Win32 SDK Programmers Reference, Volume 4.RemarksThis function sets the priority level of the current thread within its priority class. It can only be called after CreateThread successfully returns.CWinThread Overview | Class Members | Hierarchy ChartSee AlsoCWinThread:GetThreadPriority, :SetThreadPriorityTerminateThreadThe TerminateThread function terminates a thread. BOOL TerminateThread( HANDLE hThread, / handle to the thread DWORD dwExitCode / exit code for the thread); ParametershThread Handle to the thread to terminate. Windows NT: The handle must have THREAD_TERMINATE access. dwExitCode Specifies the exit code for the thread. Use the GetExitCodeThread function to retrieve a threads exit value. Return ValuesIf the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError. RemarksTerminateThread is used to cause a thread to exit. When this occurs, the target thread has no chance to execute any user-mode code and its initial stack is not deallocated. DLLs attached to the thread are not notified that the thread is terminating. TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination. For example, TerminateThread can result in the following problems: If the target thread owns a critical section, the critical section will not be released. If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the threads process could be inconsistent. If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL. A thread cannot protect itself against TerminateThread, other than by controlling access to its handles. The thread handle returned by the CreateThread and CreateProcess functions has THREAD_TERMINATE access, so any caller holding one of these handles can terminate your thread.If the target thread is the last thread of a process when this function is called, the threads process is also terminated. The state of the thread object becomes signaled, releasing any other threads that had been waiting for the thread to terminate. The threads termination status changes from STILL_ACTIVE to the value of the dwExitCode parameter. Terminating a thread does not necessarily remove the thread object from the system. A thread object is deleted when the last thread handle is closed. QuickInfoWindows NT: Requires version 3.1 or later.Windows: Requires Windows 95 or later.Windows CE: Unsupported.Header: Declared in winbase.h.Import Library: Use kernel32.lib.See AlsoProcesses and Threads Overview, Process and Thread Functions, CreateProcess, CreateThread, ExitThread, GetExitCodeThread ThreadProcThe ThreadProc function is an application-defined function that serves as the starting address for a thread. Specify this address when calling the CreateThread or CreateRemoteThread function. The LPTHREAD_START_ROUTINE type defines a pointer to this callback function. ThreadProc is a placeholder for the application-defined function name. DWORD WINAPI ThreadProc( LPVOID lpParameter / thread data); ParameterslpParameter Receives the thread data passed to the function using the lpParameter parameter of the CreateThread or CreateRemoteThread function. Return ValuesThe function should return a value that indicates its success or failure. RemarksA process can obtain the return value of the ThreadProc of a thread it created with CreateThread by calling the GetExitCodeThread function. A process cannot obtain the return value from the ThreadProc of a thread it created with CreateRemoteThread. QuickInfoWindows NT: Requires version 3.1 or later.Windows: Requires Windows 95 or later.Windows CE: Unsupported.Header: Declared in winbase.h.Import Library: User-defined.See AlsoProcesses and Threads Overview, Process and Thread Functions, CreateThread, CreateRemoteThread, GetExitCodeThread CreateMutexThe Cre

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论