当前位置: 技术问答>linux和unix
如何在windows 平台上实现消息队列(多线程环境)
来源: 互联网 发布时间:2015-07-10
本文导语: 最好给源代码,给出思路也行. msgQCreate( ) – create and initialize a message queue msgQDelete( ) – delete a message queue msgQSend( ) – send a message to a message queue msgQReceive( ) – receive a message from a message queue 1. msg...
最好给源代码,给出思路也行.
msgQCreate( ) – create and initialize a message queue
msgQDelete( ) – delete a message queue
msgQSend( ) – send a message to a message queue
msgQReceive( ) – receive a message from a message queue
1. msgQCreate
create and initialize a message queue,queue pended tasks in FIFO order
MSG_Q_ID msgQCreate
(
int maxMsgs, /* max messages that can be queued */
int maxMsgLength, /* max bytes in a message */
)
2.msgQDelete
//delete a message queue
STATUS msgQDelete
(
MSG_Q_ID msgQId /* message queue to delete */
)
3.
NAME msgQReceive( ) – receive a message from a message queue
int msgQReceive
(
MSG_Q_ID msgQId, /* message queue from which to receive */
char * buffer, /* buffer to receive message */
UINT maxNBytes, /* length of buffer */
int timeout /* ticks to wait */
)
DESCRIPTION This routine receives a message from the message queue msgQId. The received message is
copied into the specified buffer, which is maxNBytes in length. If the message is longer
than maxNBytes, the remainder of the message is discarded (no error indication is returned).
The timeout parameter specifies the number of ticks to wait for a message to be sent to the
queue, if no message is available when msgQReceive( ) is called. The timeout parameter
can also have the following special values:
NO_WAIT (0)
return immediately, even if the message has not been sent.
WAIT_FOREVER (-1)
never time out.
WARNING This routine must not be called by interrupt service routines.
RETURNS The number of bytes copied to buffer, or ERROR.
4.msgQSend( )
send a message to a message queue
STATUS msgQSend
(
MSG_Q_ID msgQId, /* message queue on which to send */
char * buffer, /* message to send */
UINT nBytes, /* length of message */
int timeout, /* ticks to wait */
)
DESCRIPTION This routine sends the message in buffer of length nBytes to the message queue msgQId. If
any tasks are already waiting to receive messages on the queue, the message will
immediately be delivered to the first waiting task. If no task is waiting to receive
messages, the message is saved in the message queue.
The timeout parameter specifies the number of ticks to wait for free space if the message
queue is full. The timeout parameter can also have the following special values:
NO_WAIT (0)
return immediately, even if the message has not been sent.
WAIT_FOREVER (-1)
never time out.
msgQCreate( ) – create and initialize a message queue
msgQDelete( ) – delete a message queue
msgQSend( ) – send a message to a message queue
msgQReceive( ) – receive a message from a message queue
1. msgQCreate
create and initialize a message queue,queue pended tasks in FIFO order
MSG_Q_ID msgQCreate
(
int maxMsgs, /* max messages that can be queued */
int maxMsgLength, /* max bytes in a message */
)
2.msgQDelete
//delete a message queue
STATUS msgQDelete
(
MSG_Q_ID msgQId /* message queue to delete */
)
3.
NAME msgQReceive( ) – receive a message from a message queue
int msgQReceive
(
MSG_Q_ID msgQId, /* message queue from which to receive */
char * buffer, /* buffer to receive message */
UINT maxNBytes, /* length of buffer */
int timeout /* ticks to wait */
)
DESCRIPTION This routine receives a message from the message queue msgQId. The received message is
copied into the specified buffer, which is maxNBytes in length. If the message is longer
than maxNBytes, the remainder of the message is discarded (no error indication is returned).
The timeout parameter specifies the number of ticks to wait for a message to be sent to the
queue, if no message is available when msgQReceive( ) is called. The timeout parameter
can also have the following special values:
NO_WAIT (0)
return immediately, even if the message has not been sent.
WAIT_FOREVER (-1)
never time out.
WARNING This routine must not be called by interrupt service routines.
RETURNS The number of bytes copied to buffer, or ERROR.
4.msgQSend( )
send a message to a message queue
STATUS msgQSend
(
MSG_Q_ID msgQId, /* message queue on which to send */
char * buffer, /* message to send */
UINT nBytes, /* length of message */
int timeout, /* ticks to wait */
)
DESCRIPTION This routine sends the message in buffer of length nBytes to the message queue msgQId. If
any tasks are already waiting to receive messages on the queue, the message will
immediately be delivered to the first waiting task. If no task is waiting to receive
messages, the message is saved in the message queue.
The timeout parameter specifies the number of ticks to wait for free space if the message
queue is full. The timeout parameter can also have the following special values:
NO_WAIT (0)
return immediately, even if the message has not been sent.
WAIT_FOREVER (-1)
never time out.
|
思路:
创建一个窗口类,本身就带有消息队列,创建多线程;
用postmessage、sendmessage来传递消息到线程中
创建一个窗口类,本身就带有消息队列,创建多线程;
用postmessage、sendmessage来传递消息到线程中