管道读写问题

  在同一进程中,如果对于同一个管道,在两个或者两个以上的线程中进行读写操作,会发生死锁现象,比如一个线程在监听读的时候,这时如果要进行写操作,将发生死锁。

解决办法,在每个线程中各创建一条管道。

这里注意一点 ,创建的管道要是双向的,而且名字要符合规定,管道两边对应的管道名字要一致

HANDLE CreateNamedPipe(
  LPCTSTR lpName,
  DWORD dwOpenMode,
  DWORD dwPipeMode,
  DWORD nMaxInstances,
  DWORD nOutBufferSize,
  DWORD nInBufferSize,
  DWORD nDefaultTimeOut,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes
);
Parameters
lpName
[in] Pointer to the null-terminated string that uniquely identifies the pipe. The string must have the following form:

//./pipe/pipename

The pipename part of the name can include any character other than a backslash, including numbers and special characters. The entire pipe name string can be up to 256 characters long. Pipe names are not case sensitive.

Windows Me/98/95:  Pipe names cannot include a colon. Therefore, if this pipe will be used from a Windows Me/98/95 client, do not include a colon in the name.
dwOpenMode
[in] Pipe access mode, the overlapped mode, the write-through mode, and the security access mode of the pipe handle.

CreateNamedPipe fails if dwOpenMode specifies any flags other than those listed in the following tables.

This parameter must specify one of the following pipe access mode flags. The same mode must be specified for each instance of the pipe.

ModeMeaning
PIPE_ACCESS_DUPLEX
0x00000003
The pipe is bi-directional; both server and client processes can read from and write to the pipe. This mode gives the server the equivalent of GENERIC_READ | GENERIC_WRITE access to the pipe. The client can specify GENERIC_READ or GENERIC_WRITE, or both, when it connects to the pipe using the CreateFile function.
PIPE_ACCESS_INBOUND
0x00000001
The flow of data in the pipe goes from client to server only. This mode gives the server the equivalent of GENERIC_READ access to the pipe. The client must specify GENERIC_WRITE access when connecting to the pipe.
PIPE_ACCESS_OUTBOUND
0x00000002
The flow of data in the pipe goes from server to client only. This mode gives the server the equivalent of GENERIC_WRITE access to the pipe. The client must specify GENERIC_READ access when connecting to the pipe.

This parameter can also include one or more of the following flags, which enable write-through mode and overlapped mode. These modes can be different for different instances of the same pipe.





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值