You Are Here:

Community: Wiki

This page was last modified on 28 September 2009, at 04:28.

Message Queues

From Forum Nokia Wiki

Reviewer Approved   

Contents

Introduction

Message Queues are one of the better and comfortable ways to send/receive messages/data between threads within a process(or a separate process).You can see other ways at Inter-Process Data Transfer and Inter-Thread Communication.

A queue is generally created to handle messages of a given type of defined(fixed) length. The size of a queue, ie, maximum number of messages, or slots, it can contain is defined and fixed when the queue is created. The size of message for which a queue is created, and the size of the queue is arbitrary, being limited only by system resources.

A Message Queue can be :

--A global queue, which is visible to all processes

--A local queue, which is local to the current process and not visible to any other process

Usage of Message Queue

The member functions defined in RMsgQueue are:

CreateGlobal(), CreateLocal(), Receive(), ReceiveBlocking(), Send(), SendBlocking()


Following are the neccessary steps to make use of a message queue:

  • Create a queue
  • Send data to the queue
  • Receive message from the queue
  • Other queue properites


Creating a Queue

RMsgQueue is the handle to a message queue.

class RMsgQueue : public RMsgQueueBase;

Here, RMsgQueueBase provides implementation for managing an asynchronous message queue, and is a base class for the RMsgQueue templated class.


class TMsgClass
{
public:
TInt iTemp1;
TInt iTemp2;
}

The following code shows how to create a global message queue with message length of the above class(Here it is 8) and with 10 such number of messages.

_LIT(KGlobalQName, "MyGlobalQ");
const TInt KNumberOfMsgs = 10;
RMsgQueue<TMsgClass> msgq;
TInt ret = msgq.CreateGlobal(KGlobalQName, KNumberOfMsgs, EOwnerProcess);


Creating a local message queue is also similar, as shown below:

const TInt KNumberOfMsgs = 2;
RMsgQueue<TMsgClass> msgq;
TInt ret = msgq.CreateLocal(KNumberOfMsgs, EOwnerProcess);


Sending data to the Queue

Using the message queue handle, a message is sent to a queue by calling Send() or SendBlocking().

Here, if the queue is full, Send() returns the error code KErrOverflow and SendBlocking() waits until there is space available in the queue.


The following code creates a global queue and sends 2 integer to values to it. To demonstrate the usage of both Send() and SendBlocking(); the first call to Send() returns an error if the queue is full, the call to SendBlocking() waits until there is space in the queue.

_LIT(KGlobalQName, "GlobalMsgQ");
RMsgQueue<TMsgClass> msgqueue;
 
//Create a global Queue
TInt ret = msgqueue.CreateGlobal(KGlobalQName,1);
 
if (ret == KErrNone) //If creation is success
{
//Using Send()
TInt value = 100;
//KErrNone, if Send() successful; KErrOverflow, if queue is full
ret = msgqueue.Send(&value);
 
//Using SendBlocking()
value = 200;
msgqueue.SendBlocking(&value);
}


Receiving Messages from the Queue

Using the message queue handle, a message is received from a message queue by calling Receive() or ReceiveBlocking(). Receive() returns the error code KErrUnderflow if the queue is empty, ReceiveBlocking() waits until there is data in the queue.

_LIT(KGlobalQName, "GlobalMsgQ");
RMsgQueue<TMsgClass> msgqueue;
TInt ret = msgqueue.OpenGlobal(KGlobalQName);
 
if (ret == KErrNone)
{
//Using Receive(), returns "KErrUnderflow" if queue is empty
TInt msgfrmq;
ret = msgqueue.Receive(&msgfrmq);
 
//Using ReceiveBlocking()
msgqueue.ReceiveBlocking(&msgfrmq);
}


Other Queue Properties

Other member functions inherited from RHandleBase are as follows:

  • CancelDataAvailable()

Cancels an outstanding data available notification request.

  • CancelSpaceAvailable()

Cancels an outstanding space available notification request.

  • KMaxLength

Gets the size of message slots in the queue.

  • MessageSize()

Gets the size of message slots in the queue.

  • NotifyDataAvailable()

Requests notification when there is at least one message in the queue. A thread can have only one data available notification request outstanding on this message queue. If a second request is made before the first request completes, then the calling thread is panicked.

  • NotifySpaceAvailable()

Requests notification when space becomes available in the queue. This is an asynchronous request that completes when there is at least one 'slot'available in the queue.

  • Open()

Opens a global message queue using a handle passed in a server message.

  • OpenGlobal()

Opens a global message queue using the name of the message queue.



--

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fHowE5ftoE5fimplementE5ftouchE5fimageE5fscrollingE5finE5fJavaE5fME45X qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxHowE20toE20implementE20touchE20imageE20scrollingE20inE20JavaE20ME45E20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtopicQUqfnTopicZjavaQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d05X qfnZuserE5ftagQSxjavaX qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ