收藏 分享(赏)

深入浅出Win32多线程程序设计.pdf

上传人:HR专家 文档编号:6300193 上传时间:2019-04-05 格式:PDF 页数:80 大小:688.31KB
下载 相关 举报
深入浅出Win32多线程程序设计.pdf_第1页
第1页 / 共80页
深入浅出Win32多线程程序设计.pdf_第2页
第2页 / 共80页
深入浅出Win32多线程程序设计.pdf_第3页
第3页 / 共80页
深入浅出Win32多线程程序设计.pdf_第4页
第4页 / 共80页
深入浅出Win32多线程程序设计.pdf_第5页
第5页 / 共80页
点击查看更多>>
资源描述

1、 0 Win32L !9 Q 2005-12-14 09:48T )? 7?3 I Z VLL T“d?ZBA t M DOS“d T“dK9 ?YV = =iZ T LC “ “ 7 Win32 T“d VBHP BHIBHub L# aYZ T C T“d1oB Y Win32L !9 T“d e9d bV V 3 T“d5 T“d VxWorks ? S 7?v1B H Win32L /b yN Win32L Win32 hProcess s c HANDLE M hModule | NULL lpstrFileName LPTSTR sf . ib 3 F nsizelpstrFile

2、Name F f R TRBv 0 DWORD H| hProcess ib lpstrFileName _ F FT 5R 0b YV/ VR “d V / | -9 EnumProcesses(process_ids, sizeof(process_ids), /R for (int i = 0; i f BOOL CloseHandle(HANDLE hObject); Tf 5R TRUE 5R FALSE VYV GetLastErrorf B Vpyb C H o VC+6.0 LIZEB P C H o# WIN32 APIf 6BZE P MFCMFC L 7? class E

3、xampleTask public: void taskmain(LPVOID param); void StartTask(); ; void ExampleTask:taskmain(LPVOID param) void ExampleTask:StartTask() _beginthread(taskmain,0,NULL); int main(int argc, char* argv) ExampleTask realTimeTask; realTimeTask.StartTask(); return 0; Ir HC /p error C2664: _beginthread : ca

4、nnot convert parameter 1 from void (void *) to void (_cdecl *)(void *) None of the functions with this name in scope match the target type A/ #include “windows.h“ #include class ExampleTask public: void taskmain(LPVOID param); ; void ExampleTask:taskmain(LPVOID param) int main(int argc, char* argv)

5、ExampleTask realTimeTask; _beginthread(ExampleTask:taskmain,0,NULL); return 0; Ir Hp error C2664: _beginthread : cannot convert parameter 1 from void (void *) to void (_cdecl *)(void *) None of the functions with this name in scope match the target type TB1 f TLf Y / %Z 1|f 2 static this | =M #inclu

6、de “windows.h“ #include class ExampleTask public: void static taskmain(LPVOID param); void StartTask(); ; void ExampleTask:taskmain(LPVOID param) void ExampleTask:StartTask() _beginthread(taskmain,0,NULL); int main(int argc, char* argv) ExampleTask realTimeTask; realTimeTask.StartTask(); return 0; #

7、include “windows.h“ #include class ExampleTask public: void static taskmain(LPVOID param); ; void ExampleTask:taskmain(LPVOID param) int main(int argc, char* argv) _beginthread(ExampleTask:taskmain,0,NULL); return 0; (IrYVb |f 2 V %TLf 5 5 * staticf ? staticb %N5Bo V f Lf H| thisT . iLf class Exampl

8、eTask public: friend void taskmain(LPVOID param); void StartTask(); ; void taskmain(LPVOID param) ExampleTask * pTaskMain = (ExampleTask *) param; /YV pTaskMain void ExampleTask:StartTask() _beginthread(taskmain,0,this); int main(int argc, char* argv) ExampleTask realTimeTask; realTimeTask.StartTask

9、(); return 0; 3 Vd f LCid NE #Bt)/ FNW b 2.7yL L T“d1 3 Win324 CreateThread API L7y API HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes,/Pointer to a SECURITY_ATTRIBUTES structure SIZE_T dwStackSize, /Initial size of the stack, in bytes. LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lp

10、Parameter, /Pointer to a variable to be passed to the thread DWORD dwCreationFlags, /Flags that control the creation of the thread LPDWORD lpThreadId /Pointer to a variable that receives the thread identifier ); T P C/C+ILB ? P T“d4 CreateThread API7P C/C+ H o_beginthread _beginthreadex f uintptr_t

11、_beginthread( void( _cdecl *start_address )( void * ), /Start address of routine that begins execution of new thread unsigned stack_size, /Stack size for new thread or 0. void *arglist /Argument list to be passed to new thread or NULL ); uintptr_t _beginthreadex( void *security,/Pointer to a SECURIT

12、Y_ATTRIBUTES structure unsigned stack_size, unsigned ( _stdcall *start_address )( void * ), void *arglist, unsigned initflag,/Initial state of new thread (0 for running or CREATE_SUSPENDED for suspended); unsigned *thrdaddr ); _beginthreadf Win32 API CreateThreadf /s 1YV_beginthread f V V arglist| .

13、L 2_beginthread f S t C H oM L 1 P C H ob 3.L L / Z T 1Lf R 2L1 | fuExitCode !L| b i T P C/C+I P C/C+ H of _endthread (_endthreadex) L % ? P ExitThread _endthread f L =Hqb Y) L E |Yge5|b 3B L TerminateThreadf BOOL TerminateThread(HANDLE hThread,DWORD dwExitCode); f hThread Li dwExitCode !L| b LY H V

14、 Lf YLb 4cLb Kz P 1Z TL 24Z TRb 4.+ L 7yL H T . CREATE_SUSPENDEDS5L7y$+ P ResumeThread DWORD ResumeThread(HANDLE hThread); TResumeThreadf |RL -B T9 5R 0x FFFFFFFFb $+ L V SuspendThreadf #include #include #include DWORD WINAPI ThreadFunc(LPVOID); int main() HANDLE hThrd1; HANDLE hThrd2; DWORD exitCod

15、e1 = 0; DWORD exitCode2 = 0; DWORD threadId; hThrd1 = CreateThread(NULL, 0, ThreadFunc, (LPVOID)1, 0, if (hThrd1) printf(“Thread 1 launchedn“); hThrd2 = CreateThread(NULL, 0, ThreadFunc, (LPVOID)2, 0, if (hThrd2) printf(“Thread 2 launchedn“); / Keep waiting until both calls to GetExitCodeThread succ

16、eed AND / neither of them returns STILL_ACTIVE. for (;) printf(“Press any key to exitn“); getch(); GetExitCodeThread(hThrd1, GetExitCodeThread(hThrd2, if ( exitCode1 = STILL_ACTIVE ) puts(“Thread 1 is still running!“); if ( exitCode2 = STILL_ACTIVE ) puts(“Thread 2 is still running!“); if ( exitCode

17、1 != STILL_ACTIVE CloseHandle(hThrd1); CloseHandle(hThrd2); printf(“Thread 1 returned %dn“, exitCode1); printf(“Thread 2 returned %dn“, exitCode2); return EXIT_SUCCESS; /* * Take the startup value, do some simple math on it, * and return the calculated value. */ DWORD WINAPI ThreadFunc(LPVOID n) Sle

18、ep(DWORD)n*1000*2); return (DWORD)n * 10; YV/ V AL 4 # WINAPI CreateThreadf C H o_beginthread Y #define WIN32_LEAN_AND_MEAN #include #include #include DWORD WINAPI ThreadFunc(LPVOID); int main() HANDLE hThrd; DWORD threadId; int i; for (i = 0; i #include #include unsigned Counter; unsigned _stdcall

19、SecondThreadFunc(void *pArguments) printf(“In second thread.n“); while (Counter %dn“, Counter); / Destroy the thread object. CloseHandle(hThread); 0 Win32L !9LY 2005-12-16 08:48T )? 7?3 I Z e LWY 5 b L LW B1“BLG 6BLh 6BLh Hhr H$b L Z T“d yN VOID Rbf A LEnterCriticalSectionf -$ TBL km B S CRTICAL_SEC

20、TION * T| 49b “ “ u VOID WINAPI DeleteCriticalSection( LPCRITICAL_SECTION lpCriticalSection /_B1 CRITICAL_SECTIONM ); “ u VOID WINAPI EnterCriticalSection( LPCRITICAL_SECTION lpCriticalSection /_B F| CRITICAL_SECTIONM ); 7 “ u VOID WINAPI LeaveCriticalSection( LPCRITICAL_SECTION lpCriticalSection /_

21、B F| 7 CRITICAL_SECTIONM ); P “ uIBZE void UpdateData() EnterCriticalSection( ./do something LeaveCriticalSection( 1 “ u P/ i 1 Z PB CRITICAL_SECTIONM 21 HW1o B1o HW H L ? 3 T1 H5 V ? EnterCriticalSection 4Critical Section OS T “ uL“ +“ |E db “b Mutex b T Q ?BL 7? P CreateMutexf 7y HANDLE CreateMute

22、x( LPSECURITY_ATTRIBUTES lpMutexAttributes, / V NULL BOOL bInitialOwner, / TRUE FALSE LPCTSTR lpName /| ); Mutex V t/ V 6B Mutex 0 HANDLE hMutex; hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, L“mutexName“); if (hMutex) b else M1API BOOL WINAPI ReleaseMutex( HANDLE hMutex ); PIBZE void UpdateResource()

23、 WaitForSingleObject(hMutex,); ./do something ReleaseMutex(hMutex); (mutex) = ? L b+ “ uM =7 “ u5 Z TyN mutex Critical Section/ 1 11o 1 2 L ? 3 L H V !B Hb /mW% “ u | | 0 KvWb| 9 v 0 H |7 9 0 H |b| e VK Zb | +o V/ + l 1 T - v 05 | r 2 T - 05| r 3“d % - 4 - % ?vKv b 7y| HANDLE CreateSemaphore ( PSECU

24、RITY_ATTRIBUTE psa, LONG lInitialCount, / 7 S H V P LONG lMaximumCount, /Kv PCTSTR pszName); db| YV ReleaseSemaphoref L ?S - 9f BOOL WINAPI ReleaseSemaphore( HANDLE hSemaphore, LONG lReleaseCount, /| - 9F lReleaseCount LPLONG lpPreviousCount ); u 7| B“| 9 VYV 3 tu 7| API HANDLE OpenSemaphore ( DWORD

25、 fdwAccess, BOOL bInherithandle, PCTSTR pszName ); A0 TZ T H f Mb 0 L H ? LB HW =Mb h A/ int globalVar = 0; DWORD WINAPI ThreadFunc1(LPVOID n) globalVar+; return 0; DWORD WINAPI ThreadFunc2(LPVOID n) globalVar+; return 0; ThreadFunc1 ThreadFunc2LT V y globalVar+i“BH 7 A A globalVar+QI 00401038 mov e

26、ax,globalVar (0042d3f0) 0040103D add eax,1 00401040 mov globalVar (0042d3f0),eax “mov eax,globalVar (0042d3f0)“ 7 “add eax,1“ 7#“add eax,1“ 7“mov globalVar (0042d3f0),eax“ 7W V ? 3L M P globalVarT ? b V P InterlockedExchangeAddf %5 int globalVar = 0; DWORD WINAPI ThreadFunc1(LPVOID n) InterlockedExc

27、hangeAdd( return 0; DWORD WINAPI ThreadFunc2(LPVOID n) InterlockedExchangeAdd( return 0; InterlockedExchangeAdd M globalVar “ 0“ b e d yB f CPU Yl 501Z T =Z T M MY1 1000 CPU b f ?BM 0 T11 1 P “ ub V H V H HW?W HW?1-|Y =b Y HW Tb 7y V H HANDLE CreateWaitableTimer( PSECURITY_ATTRISUTES psa, BOOL fManu

28、alReset,/ 1 H PCTSTR pszName); ! V H V H d /$7y SetWaitableTimer f H H$ BOOL SetWaitableTimer( HANDLE hTimer, /1 ! H const LARGE_INTEGER *pDueTime, / H BQ HW LONG lPeriod, / N H W HWBQ PTIMERAPCROUTINE pfnCompletionRoutine, PVOID PvArgToCompletionRoutine, BOOL fResume); |h V H BOOl Cancel WaitableTi

29、mer( HANDLE hTimer /1 |h H ); u 7 V H TB =WaitableTimer 9 V$ 3u 7 HANDLE OpenWaitableTimer ( DWORD fdwAccess, BOOL bInherithandle, PCTSTR pszName ); L / B V ? 3 C #include #include CRITICAL_SECTION cs1, cs2; long WINAPI ThreadFn(long); main() long iThreadID; InitializeCriticalSection( InitializeCrit

30、icalSection( CloseHandle(CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadFn, NULL, 0, while (TRUE) EnterCriticalSection( printf(“nL 1 “ u 1“); EnterCriticalSection( printf(“nL 1 “ u 2“); printf(“nL 1 “ u“); LeaveCriticalSection( LeaveCriticalSection( printf(“nL 1 db “ u“); Sleep(20); ; return (0); long WINAPI ThreadFn(long lParam) while (TRUE) EnterCriticalSection( printf(“nL 2 “ u 2“); EnterCriticalSection( printf(“nL 2 “ u 1“); printf(“nL 2 “ u“); LeaveCriticalSection( LeaveCriticalSection( printf(“nL 2 d

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报