#include "stdafx.h"
#include "windows.h"
//----------------------------------------------------------------------
DWORD WINAPI Reader( LPVOID lpParam ) {
HANDLE hReadPipe=(HANDLE)lpParam;
while (true) {
char buffer[1024];
DWORD dw;
BOOL b = ReadFile(hReadPipe, buffer, sizeof(buffer), &dw, NULL);
printf( "Reader res=%d dw=%d\n", b, dw );
}
return 0;
}
//----------------------------------------------------------------------
DWORD WINAPI Writer( LPVOID lpParam ) {
HANDLE hWritePipe=(HANDLE)lpParam;
while (true) {
DWORD dw;
BOOL b = WriteFile(hWritePipe, "abcd", 4, &dw, NULL);
printf( "Writer res=%d dw=%d\n", b, dw);
Sleep(5000);
}
}
//----------------------------------------------------------------------
int main(int argc, char* argv[]) {
HANDLE hReadPipe;
HANDLE hWritePipe;
BOOL b = CreatePipe(&hReadPipe, &hWritePipe, NULL, 1024);
printf( "CreatePipe res=%d\n", b );
DWORD id;
CreateThread(NULL,0,Reader,(LPVOID)hReadPipe,NULL,&id);
CreateThread(NULL,0,Writer,(LPVOID)hWritePipe,NULL,&id);
while (true) {
DWORD avail;
printf( "+peek\n" );
BOOL b = PeekNamedPipe( hReadPipe, NULL, 0, NULL, &avail, NULL );
printf( "-peek res=%d avail=%d\n", b, avail );
}
return 0;
}
|