扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:宋宝华 来源:天极开发 2007年10月16日
关键字:
BOOL SetThreadPriority(HANDLE hThread, int nPriority); |
HANDLE hCurrentThread = GetCurrentThread(); //获得该线程句柄 SetThreadPriority(hCurrentThread, THREAD_PRIORITY_LOWEST); |
VOID Sleep(DWORD dwMilliseconds); |
Int GetThreadPriority (HANDLE hThread); |
BOOL WINAPI GetExitCodeThread( HANDLE hThread, LPDWORD lpExitCode ); |
获得/设置线程上下文 BOOL WINAPI GetThreadContext( HANDLE hThread, LPCONTEXT lpContext ); BOOL WINAPI SetThreadContext( HANDLE hThread, CONST CONTEXT *lpContext ); |
#define WIN32_LEAN_AND_MEAN #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <conio.h> DWORD WINAPI ThreadFunc(LPVOID); int main() { HANDLE hThrd1; HANDLE hThrd2; DWORD exitCode1 = 0; DWORD exitCode2 = 0; DWORD threadId; hThrd1 = CreateThread(NULL, 0, ThreadFunc, (LPVOID)1, 0, &threadId ); if (hThrd1) printf("Thread 1 launched\n"); hThrd2 = CreateThread(NULL, 0, ThreadFunc, (LPVOID)2, 0, &threadId ); if (hThrd2) printf("Thread 2 launched\n"); // Keep waiting until both calls to GetExitCodeThread succeed AND // neither of them returns STILL_ACTIVE. for (;;) { printf("Press any key to exit..\n"); getch(); GetExitCodeThread(hThrd1, &exitCode1); GetExitCodeThread(hThrd2, &exitCode2); if ( exitCode1 == STILL_ACTIVE ) puts("Thread 1 is still running!"); if ( exitCode2 == STILL_ACTIVE ) puts("Thread 2 is still running!"); if ( exitCode1 != STILL_ACTIVE && exitCode2 != STILL_ACTIVE ) break; } CloseHandle(hThrd1); CloseHandle(hThrd2); printf("Thread 1 returned %d\n", exitCode1); printf("Thread 2 returned %d\n", exitCode2); return EXIT_SUCCESS; } /* * Take the startup value, do some simple math on it, * and return the calculated value. */ DWORD WINAPI ThreadFunc(LPVOID n) { Sleep((DWORD)n*1000*2); return (DWORD)n * 10; } |
#define WIN32_LEAN_AND_MEAN #include <stdio.h> #include <stdlib.h> #include <windows.h> DWORD WINAPI ThreadFunc(LPVOID); int main() { HANDLE hThrd; DWORD threadId; int i; for (i = 0; i < 5; i++) { hThrd = CreateThread(NULL, 0, ThreadFunc, (LPVOID)i, 0, &threadId); if (hThrd) { printf("Thread launched %d\n", i); CloseHandle(hThrd); } } // Wait for the threads to complete. Sleep(2000); return EXIT_SUCCESS; } DWORD WINAPI ThreadFunc(LPVOID n) { int i; for (i = 0; i < 10; i++) printf("%d%d%d%d%d%d%d%d\n", n, n, n, n, n, n, n, n); return 0; } |
#include <Win32.h> #include <stdio.h> #include <process.h> unsigned Counter; unsigned __stdcall SecondThreadFunc(void *pArguments) { printf("In second thread...\n"); while (Counter < 1000000) Counter++; _endthreadex(0); return 0; } int main() { HANDLE hThread; unsigned threadID; printf("Creating second thread...\n"); // Create the second thread. hThread = (HANDLE)_beginthreadex(NULL, 0, &SecondThreadFunc, NULL, 0, &threadID); // Wait until second thread terminates WaitForSingleObject(hThread, INFINITE); printf("Counter should be 1000000; it is-> %d\n", Counter); // Destroy the thread object. CloseHandle(hThread); } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者