我刚开始在Windows中做一些管道。所以我为基本的READ / WRITE通信编写了一些示例代码。但ReadFile是不间断的阻塞,永远不会读取数据。
HANDLE Oprimek_Read;
HANDLE Oprimek_Write;
SECURITY_ATTRIBUTES varnostniAtributi;
varnostniAtributi.nLength = sizeof(SECURITY_ATTRIBUTES);
varnostniAtributi.bInheritHandle = TRUE;
varnostniAtributi.lpSecurityDescriptor = NULL;
thread prvi (pisanje);
thread drugi (branje);
BOOL ustvarjenaCev = FALSE;
ustvarjenaCev = CreatePipe(&Oprimek_Read,&Oprimek_Write,&varnostniAtributi,150);
if(ustvarjenaCev == TRUE)
printf("Anon pipe created\n");
else
printf("Error creating pipe\n");
DWORD dwWritten;
DWORD dwBytesToWrite = (DWORD)strlen(buffer);
cin>>beseda;
strcpy(buffer,beseda.c_str());
printf("Buffer to send is: %s\n", buffer);
ustvarjenaCev = WriteFile(Oprimek_Write,buffer,dwBytesToWrite,&dwWritten,NULL);
FlushFileBuffers(Oprimek_Write);
FlushFileBuffers(Oprimek_Read);
if( ! ustvarjenaCev){
ErrorExit(TEXT("Write to Pipe"));
}
//-----------------HERE IS THE PROBLEM--------------
cout<<"Waiting\n";
ustvarjenaCev = ReadFile(Oprimek_Read,buffer1,sizeof(buffer),0,NULL);
cout<<"ReadPipe\n";
if( ! ustvarjenaCev){
ErrorExit(TEXT("Read to pipe"));
}
else
cout<<buffer1<<endl;
我认为我已经完成了通过MSDN文档所要求的所有内容。有什么想法吗?