Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I have a requirement for creating a Event based Multi-thread application for which i am trying to use boost::thread and boost/interprocess/ipc/message_queue for sending messages between threads. What i am doing currently is making the thread wait in its workerfunction to wait for a message. Actually this is just for basic start where the sender and reciever both is a same thread, on later stage i have thought to store a list of message_queue corresponding for each thread and then fetch it accordingly or something like that. But now, as per the code below i am using

//in a common class

typedef struct s_Request{
int id;
}st_Request;


//in thread(XYZ) class
st_Request dataone;
message_queue *mq;

void XYZ::threadfunc(void *ptr)
{

  XYZ*obj = (XYZ*) ptr;
  obj->RecieveMsg();

}

void XYZ::RecieveMsg()
{
  message_queue mq1(open_only,"message_queue");
  if(!(mq1.try_receive(&dataone, sizeof(st_Request), recvd_size, priority)))
  printf("msg not recieved");

  printf("id = %d",dataone.id);
}
void XYZ::Create()
{
  mq= new message_queue(open_or_create,"message_queue",100,sizeof(st_Request));
  boost:thread workerthread(threadfunc,this);
  workerthread.join();
}

void XYZ::Send(st_Request *data)
{

  if (!(mq->try_send(data, sizeof(st_Request), 0)))
  printf("message sending failed");

}

//I am calling it like
class ABC: public XYZ
{
 ..some functions to do stuff... };
void ABC::createMSGQ()
{
  create();
  st_Request *data;
  data->id =10;
  send(data);
}

My thread is waiting in RecieveMsg but i am not getting any msg and the prints are coming till Send function entry and than the code crash. Please Guide me for what i am doing wrong, if the approach is entirely wrong, i am open to move to new approach.

P.s. this is my first question on stack overflow i tried follow the guidelines still if i strayed away anywhere please do correct.

share|improve this question
    
You should indent the code properly so it is readable. You can paste code in then highlight it and use Ctrl-K (or the {} icon) to indent the whole block consistently. Anyway, what is threadfunc? Why do you pass the argument to XYZ::ThreadFunc as void* instead of XYZ*? – Jonathan Wakely Sep 11 '14 at 14:35
    
@JonathanWakely - ThreadFunc is the Function from where the workerthread created starts its execution on being created. from here - boost:thread workerthread(threadfunc,this). And to be honest void * , was just passed following an example and later i cast it to XYZ* to access other member functions as ThreadFunc is a static member function. – Devansh Sep 11 '14 at 14:42
    
I didn't ask what ThreadFunc is, I asked what threadfunc is, which you use in Create. In C++ threadfunc is not the same as ThreadFunc. Please don't ask questions with nonsense code, it wastes everyone's time. Paste code that actually compiles or the question will get closed. – Jonathan Wakely Sep 11 '14 at 14:45
    
@JonathanWakely my bad. – Devansh Sep 11 '14 at 14:47

1 Answer 1

st_Request *data;
data->id =10;

data is uninitialized, you cannot dereference it. Pointers should point to something before you dereference them.

I don't understand the point of this function:

void XYZ::Create()
{
  mq= new message_queue(open_or_create,"message_queue",100,sizeof(st_Request));
  boost:thread workerthread(threadfunc,this);
  workerthread.join();
}

You create a new thread, then block and wait for it to finish so you can join it. Why not just do the work here, instead of creating a new thread and waiting for it to finish?

What is threadfunc? Do you mean ThreadFunc?

This function is written strangely:

void XYZ::ThreadFunc(void *ptr)
{
  XYZ*obj = (XYZ*) ptr;
  obj->RecieveMsg();
}

Why not pass the argument as XYZ* instead of void*? Boost.Thread doesn't require everything to be passed as void*. Is that function static? It doesn't need to be:

struct XYZ {
  void threadFunc();
  void create();
  void recv();
};

void XYZ::threadFunc()
{
  recv();
}

void XYZ::create()
{
  boost::thread thr(&XYZ::threadFunc, this);
  thr.join();
}
share|improve this answer
    
ThreadFunc is my bad. its threadfunc only. and in RecieveMsg actually i am trying to achieve is that a thread is always waiting for a message as soon as it receives it does some stuff its supposed to do. I am not so sure now wethear or not i am going on the right approach. – Devansh Sep 11 '14 at 14:52
    
Your approach makes no sense, for the reasons given above. Creating a new thread and joining it immediately is useless. – Jonathan Wakely Sep 11 '14 at 15:02
    
thanks for the input, i would work on the points you mentioned, and may get back with another clarified approach hopefully. – Devansh Sep 11 '14 at 15:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.