Nokia N97 SDK
Example Applications Guide

MsgQClient.c File Reference


Detailed Description

Description: A multithread program used for Testing MsgQLib library. This application will create 2 message queues and creates 2 threads passing them the queue id. These threads will send and receive messages to each other. Copyright (c) 2007 Nokia Corporation. This material, including documentation and any related computer programs, is protected by copyright controlled by Nokia Corporation.

Definition in file MsgQClient.c.

#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "MsgQLib.h"

Go to the source code of this file.


Functions

void  ThreadFunction (int *id)
int  main ()

Variables

ULONG  q1 = 1
ULONG  q2 = 2
const int  KMaxMsg = 10


Function Documentation

void ThreadFunction (  int *  id  ) 
 

Entry point for threads This will send message to one queue and wait for some messages from other queue

Definition at line 29 of file MsgQClient.c.

References KMaxMsg, MSG_PRI_NORMAL, MSG_Q_FIFO, MsgQCreate(), MsgQReceive(), MsgQSend(), NO_WAIT, q1, and q2.

Referenced by main().

00029                              {
00030         int i = 2;
00031         int err = 0;
00032         int result = 0;
00033         int pri = MSG_PRI_NORMAL;
00034         int timeout = NO_WAIT;
00035         int len = 100;
00036         int nBytes;
00037         char smsg[] =  "Sending Some Data through MsgQ";
00038         char rmsg[100];
00039         ULONG queueOne = q1;
00040         ULONG queueTwo = q2;
00041         
00042         //If 2nd Thread
00043         if(*id == q2) {
00044                 queueOne = q2;
00045                 queueTwo = q1;
00046                 }
00047 
00048         printf("Thread %d: Started..\n", *id);
00049 
00050         /* Try to Create queueOne again, this will create the queue again,
00051            this will just return as its already created by main thread */
00052         result = MsgQCreate(queueOne, KMaxMsg, MSG_Q_FIFO, &err);
00053         printf("Thread %d: Q CREATE result = %d\n", *id, result);
00054 
00055         while(i>=0) {
00056         
00057                 nBytes = strlen(smsg); 
00058                 /* Send Message to queueOne */
00059                 result = MsgQSend(queueOne, smsg, nBytes, pri, timeout, &err); 
00060                 printf("Thread %d: Q SEND result = %d\n", *id, result);
00061 
00062                 sleep(1);
00063                 rmsg[0] = '\0';
00064                 /* Receive Message from queueTwo */
00065                 result = MsgQReceive(queueTwo, rmsg, len, timeout, &err); 
00066                 rmsg[result] = '\0';
00067                 printf("Thread %d: Q RECEIVE result = %d\n", *id, result);
00068                 printf("Thread %d: Message Received from Message Queue 2 is : %s\n", *id, rmsg);
00069                 i--;
00070         }
00071         sleep(2);
00072         
00073         /* delete message queueOne */
00074         result=MsgQDelete(queueOne, &err);
00075         printf("Thread %d: Q DELETE result = %d\n", *id, result);
00076 }


© Nokia 2009

Back to top