1

I'm using mqueue to communicate between threads and I'm having a problem passing objects.

The mq_send and mq_receive take a char* as a parameter for the object.

I use them the following way.

foo* foo = new foo();
foo->set_id(3);
mq_send(myQueue, (char*)foo, 1024, 1);

then

char* buffer;
while(true)
{
    ssize_t bytes_read;
    bytes_read = mq_receive(myQueue, buffer, 1024, NULL);
    foo* foo = (foo*) buffer;
    foo->get_id(); //equals 3
    //Send the object to another queue
    mq_send(myOtherQueue, buffer, 1024, 1);
}

So far so good.

The problem is here

    char* buffer;
    while(true)
    {
        ssize_t bytes_read;
        bytes_read = mq_receive(myOtherQueue, buffer, 1024, NULL);
        foo* foo = (foo*) buffer;
        foo->get_id(); //equals garbage 323234234
    }

The second time I cast the buffer, I get garbage. I read about static_cast and dynamic_cast and I can't find what's the problem.

What is wrong?

Pharap
  • 3,826
  • 5
  • 37
  • 51
Marc
  • 16,170
  • 20
  • 76
  • 119

1 Answers1

4

I see two issues here. First, is your Foo TriviallyCopyable?

Second,

char* buffer;
bytes_read = mq_receive(myOtherQueue, buffer, 1024, NULL);

I do not see any allocation for buffer.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • Thank you @SergeyA. I don't understand your second point. mq_receive sets the buffer variable – Marc Dec 05 '16 at 16:36
  • @Marc, no, it copies data to the buffer provided. But your buffer is not allocated. – SergeyA Dec 05 '16 at 16:37
  • Is it not done in mq_receive based on the lenght (1024)? – Marc Dec 05 '16 at 16:42
  • Basically, all I want is share an address to an object between multiple threads – Marc Dec 05 '16 at 16:44
  • No, it is not done. It expects an allocated buffer (as every other similar function). Also, using mq in intra-process communication is a terrible waste of efficiency. It is am IPC mechanism. – SergeyA Dec 05 '16 at 16:48
  • I'm new to C++. Could you give me an example and what would you use instead of mq_queue? – Marc Dec 05 '16 at 16:51
  • You need to start looking for thread queues. There is plenty of material out there. – SergeyA Dec 05 '16 at 16:52
  • Thank you very much! I just don't understand why it works for the first cast but not the second one – Marc Dec 05 '16 at 16:53
  • 1
    @Marc It was already explained - you didn't allocate any memory for your `buffer`, and didn't even initialize it - it will point to an indeterminate location in memory, hence writing data to it is Undefined behavior (it may _appear_ to work, or it may not). – Algirdas Preidžius Dec 05 '16 at 16:58
  • @AlgirdasPreidžius No buffer points to foo*, it's not undefined – Marc Dec 05 '16 at 18:02
  • @Marc No, it doesn't. Please Re-read the answer, and all of the comments again. Since I don't plan to repeating myself over, and over again. – Algirdas Preidžius Dec 05 '16 at 18:03
  • You are right. It works when I do char buffer[1024]. I just don't understand why it was working in the first thread. – Marc Dec 05 '16 at 18:09