zoukankan      html  css  js  c++  java
  • 并发学习之:APC (异步过程调用)

    一个简单的APC例子,对应于Windows 并发编程指南p124。


    #include "stdafx.h"
    #include <stdio.h>
    #include <windows.h>
    
    
    VOID NTAPI ApcFunc(ULONG_PTR pValue)
    {
    	printf("Hello, this is APC and the parameter value is %d\n", *((DWORD*)pValue));
    	delete (DWORD*)pValue;
    }
    
    DWORD WINAPI Thread_A_Proc(LPVOID)
    {
    	printf("Thread A is waiting...\n");
    
    	// Note: hEvent never signals
    	HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE/*bInitialState*/, NULL);
    
    	// Wait till APC calls
    	DWORD dwRet = WaitForSingleObjectEx(hEvent, INFINITE, TRUE /*bAlertable*/);
    	if(dwRet == WAIT_IO_COMPLETION)
    		printf("Hoho, A is waken up by APC call.\n");
    
    	CloseHandle(hEvent);
    	return 0L;
    }
    
    DWORD WINAPI Thread_B_Proc(LPVOID hThread_A)
    {
    	printf("Thread B will wake up Thread A in 3 sec...\n");
    
    	Sleep(3000);
    	DWORD* dwData = new DWORD(2013);
    	QueueUserAPC(ApcFunc, (HANDLE)hThread_A, (ULONG_PTR)dwData);
    	return 0L;
    }
    
    int wmain(int argc, wchar_t * argv[])
    {
    	HANDLE handles[2] = {};
    
    	// 1) Create two thread:
    	//        A. Sleep and alertable wait.
    	//        B. Send a APC to A's APC queue.
    	HANDLE hThread_A = handles[0] = CreateThread(NULL, 0, Thread_A_Proc, NULL, 0, NULL);
    	HANDLE hThread_B = handles[1] = CreateThread(NULL, 0, Thread_B_Proc, hThread_A, 0, NULL);
    
    	WaitForMultipleObjects(2, handles, TRUE, INFINITE);
    
    	printf("Done!\n");
    	getchar();
    }


    执行结果:

    Thread A is waiting...
    Thread B will wake up Thread A in 3 sec...
    Hello, this is APC and the parameter value is 2013
    Hoho, A is waken up by APC call.
    Done!
    


  • 相关阅读:
    Discuz经典函数注释之authcode
    在delphi中,DLL加载时做初始化的Demo
    KERNEL32相关函数
    解析 png 图片的十六进制字符流
    delphi 获取硬盘序列号、cpu号、bios号、网卡号
    封装 INI 文件读写函数
    A窗口消失B窗口弹出
    delphi公用函数
    获取打开文件的PID
    C# cmd调用外部命令
  • 原文地址:https://www.cnblogs.com/puncha/p/3876948.html
Copyright © 2011-2022 走看看