一、线程相关API介绍
在多线程操作系统中,进程是资源分配的最小单位,线程是处理器调度的最小单位,进程分配的CPU时间片又会按优先级分配给各个子线程。适当的使用多线程可以有效缩短程序完成任务的时间。在UI编程中,需要很长时间执行的后台程序放在线程中,可以保持UI界面对用户的响应。
1.创建线程: HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD(安全属性) SIZE_T dwStackSize, // initial stack size LPTHREAD_START_ROUTINE lpStartAddress, // thread function(VOID *ThreadProc(PVOID)) LPVOID lpParameter, // thread argument DWORD dwCreationFlags, // creation option (决定创建之后是立即执行还是挂起) LPDWORD lpThreadId // thread identifier ); 2.挂起线程: VOID Sleep( DWORD dwMilliseconds // sleep time ); 3.退出/终止线程: VOID ExitThread( // 退出当前线程 DWORD dwExitCode // exit code for this thread ); BOOL TerminateThread( // 在主线程中结束指定线程,导致目标线程退出,使用时要注意。 HANDLE hThread, // handle to thread DWORD dwExitCode // exit code ); 4.获取/设置线程优先级: int GetThreadPriority( HANDLE hThread // handle to thread ); BOOL SetThreadPriority( HANDLE hThread, // handle to the thread int nPriority // thread priority level ); 线程优先级nPriority参数取值,线程优先级和线程的基本优先级共同决定当前线程可以分配到多少CPU时间片。 #define THREAD_PRIORITY_LOWEST THREAD_BASE_PRIORITY_MIN #define THREAD_PRIORITY_BELOW_NORMAL (THREAD_PRIORITY_LOWEST+1) // 正常之下,比最低优先级高1级 #define THREAD_PRIORITY_NORMAL 0 // 正常 #define THREAD_PRIORITY_HIGHEST THREAD_BASE_PRIORITY_MAX #define THREAD_PRIORITY_ABOVE_NORMAL (THREAD_PRIORITY_HIGHEST-1) // 正常之上,比最高优先级低1级 #define THREAD_PRIORITY_ERROR_RETURN (MAXLONG) #define THREAD_PRIORITY_TIME_CRITICAL THREAD_BASE_PRIORITY_LOWRT #define THREAD_PRIORITY_IDLE THREAD_BASE_PRIORITY_IDLE 5.获取当前线程/线程ID: HANDLE GetCurrentThread(VOID); // 获取当前线程句柄 DWORD GetCurrentThreadId(VOID); // 获取当前线程ID 6.示例代码: 下面代码演示了上面的部分函数的使用方法,主线程是执行main函数,main函数中创建了两个线程分别执行两个函数,这两个函数功能基本相同打印从1到传入的int参数。 #include <stdio.h> #include <windows.h> DWORD ThdProc1(PVOID param) { int times = *(int *)param; int i; for (i = 0; i < times; ++i) { Sleep(1); printf("Thread 1: %d\n", i); if(i == times/2) { //Exit thread with DWORD type code which can be got by GetExitCodeThread() function. ExitThread(99); } } } DWORD ThdProc2(PVOID param) { int times = *(int *)param; int i; for (i = 0; i < times; ++i) { Sleep(1); printf("Thread 2: %d\n", i); } } int main(int argc, char *argv[]) { HANDLE handle1,handle2; DWORD dwIDThread, dwExitCode; int times = 100; // Create a thread. handle1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThdProc1, (PVOID)×, 0, &dwIDThread); // Set thread priority SetThreadPriority(handle1, THREAD_PRIORITY_LOWEST); // Create a thread with suspend state.It will not run utill the ResumeThread function is called. handle2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThdProc2, (PVOID)×, 0, &dwIDThread); // Suspend the primary thread a while. Sleep(times * 3); // Get the exit code setted by handle1 GetExitCodeThread(handle1, &dwExitCode); printf("Thread1 exit with code:%d\n", dwExitCode); // Get thread priority printf("Thread1 priority:%d\n", GetThreadPriority(handle1)); printf("Thread2 priority:%d\n", GetThreadPriority(handle2)); // Resume the suspended thread. // Note: need the handle2 have the THREAD_SUSPEND_RESUME access to the thread.see the Thread Security and Access rights //ResumeThread(handle2); return 0; }
二、C语言启用线程函数
微软对C run-time library的扩充使其支持线程_beginthread和_endthread,是指更容易使用,这些函数定义在process.h头文件中。 unsigned long _beginthread( void( __cdecl *start_address )( void * ), // 在线程中执行的函数地址 unsigned stack_size, // 分配线程堆栈大小,置0使用默认 void * arglist ); // 传入线程执行函数的的参数地址 void _endthread( void ); // 终止当前线程 下面代码演示如何使用,示例代码: #include <stdio.h> #include <process.h> void ThdProc(void *pParam) { int times = *(int *)pParam; int i; for (i = 0; i < times; ++i) { printf("Thread 1: %d\n", i); } // End current thread. _endthread(); } int main(int argc, char *argv[]) { int nTimes = 100; int i; // Begin a new thread _beginthread(ThdProc, 0, &nTimes); for (i = 0; i < nTimes*2; ++i) { printf("Thread 2: %d\n", i); } return 0; }