2009-04-16 42 views
0

如果我想用完成端口從不同的線程獲取信息,關於IOCP的問題

我該如何設計程序的結構?下面的怎麼樣?

如果我想使用全局函數,該如何設置互斥鎖?

Main(){ 
    for i in range NumOfThreads{ 
    CreateIoCompletionPort() 
    CreatThread(ThreadFun) 
    } 
} 

ThreadFun(){ 

    While(1){ 
     GetQueuedCompletionStatus(); // wait for completion of an IO 
     Process What ever has completed(); 
     Start another file operation(); 
    } 

} 

回答

0

嘗試這種解決方案:

Main(){ 
    for i in range NumOfThreads{ 
    CreateIoCompletionPort() 
    CreateThread(ThreadFun) 
    } 

    for i in range NumOfCallerThreads 
    CreateThread(ThreadCaller) 
} 

ThreadCaller(){ 
    While(1){ 
    Start another file operation(); 
    } 
} 


ThreadFun(){ 
    While(1){ 
     GetQueuedCompletionStatus(); // wait for completion of an IO 
     Process What ever has completed(); 
    } 
} 

你可以做到這一點沒有任何重要的部分!您所需要的只是保證「啓動另一個文件操作();」在關閉相應的IOCP後不會被調用。