Threads and Pipes in Console Apps
2008-07-09 22:31
267 查看
原文链接:Threads and Pipes in Console Apps
控制台程序中的线程和管道
问题是:如何创建一个可能阻塞的程序,但在阻塞的时候能当数据可读的时候从stdour和stderr中接收数据。本文的目的是展示如何在控制台程序中使用多线程。
子进程程序:
int _tmain(int argc, _TCHAR* argv[])
程序创建了两个线程:一个线程处理stdout,另一个线程处理stdin,主线程从两个工作线程收集数据并显示数据,当两个工作线程都结束时,主线程就结束。现在的问题是用什么机制来实现。作者使用了进程间队列机制,I/O完成端口,
class CommandLine
父进程程序:
int _tmain(int argc, _TCHAR* argv[])
智能句柄类
用来处理句柄的生存期问题:
class SmartHandle
线程参数类:
class ThreadParms
读线程工作函数
UINT __stdcall reader(LPVOID p)
class PipeReader
小结
子进程往管道的两个写端口写数据,父进程从管道的两个读端口读数据,为了读数据,父进程创建了两个子线程,一个子线程从stdout中读数据,一个子线程从stderr中读数据,为了能异步读取数据,使用了I/O完成端口。
控制台程序中的线程和管道
问题是:如何创建一个可能阻塞的程序,但在阻塞的时候能当数据可读的时候从stdour和stderr中接收数据。本文的目的是展示如何在控制台程序中使用多线程。
子进程程序:
int _tmain(int argc, _TCHAR* argv[])
程序创建了两个线程:一个线程处理stdout,另一个线程处理stdin,主线程从两个工作线程收集数据并显示数据,当两个工作线程都结束时,主线程就结束。现在的问题是用什么机制来实现。作者使用了进程间队列机制,I/O完成端口,
class CommandLine
父进程程序:
int _tmain(int argc, _TCHAR* argv[])
智能句柄类
用来处理句柄的生存期问题:
class SmartHandle
线程参数类:
class ThreadParms
DWORDNumberOfBytesTransferred | ULONG_PTRCompletionKey | LPOVERLAPPEDOverlapped | Meaning |
SourceFlags::StdOut | 0 | (LPOVERLAPPED)(CString *) | stdoutline to display |
SourceFlags::StdErr | 0 | (LPOVERALLPED)(CString *) | stderrline to display |
0 | SourceFlags::StdOut | NULL | stdouthas terminated |
0 | SourceFlags::StdErr | NULL | stderrhas terminated |
UINT __stdcall reader(LPVOID p)
class PipeReader
小结
子进程往管道的两个写端口写数据,父进程从管道的两个读端口读数据,为了读数据,父进程创建了两个子线程,一个子线程从stdout中读数据,一个子线程从stderr中读数据,为了能异步读取数据,使用了I/O完成端口。
相关文章推荐
- Threads and Pipes in Console Apps
- Protecting resources in iPhone and iPad apps
- Get the command parameters in Win app and Console app
- Building and packaging mobile apps in Dreamweaver CC
- How use Instruments and display the console in Command Lines applications
- Oracle EBS - What happening when executing adpreclone.pl in DB and Apps Tier?
- Enabling the Oozie Web Console and use MySQL Database in Cloudera Manager 5.2
- Managing Processes and Threads in Windows Forms
- 4 ways to do concurrency in Java: Threads, Executors, ForkJoin and Actors
- Advanced Techniques To Avoid And Detect Deadlocks In .NET Apps(线程死锁和检测-译【待续】)
- Synchronizing Threads and GUI in Delphi application
- Coloring the Console Application (AddingText and Background color in C#)
- SerialConsoles and Consoles in General
- PhoneGap: open source development framework for building cross-platform mobile apps, Build apps in HTML and JavaScript
- How use Instruments and display the console in Command Lines applications
- [转]Localize your apps and content more easily – new formats in Translator Toolkit
- Build Web Apps in Node and Express视频下载
- Run Android apps in Windows, OS X, Linux with Chrome (and a modified runtime)
- processes, threads and signals in Linux
- Processes and Threads in android