如何为两个线程定义结构的全局缓冲区

时间:2018-12-10 13:46:04

标签: qt struct global-variables buffer global

我在Qt中有两个类,生产者类和消费者类。我有一个在类Producer中的结构。我想将此结构保存在一个全局缓冲区中,并在global1.h中定义。稍后,Consumer类使用此缓冲区。 Producer和Consumer类充当两个线程。

在Producer.cpp中

       msg_t msg; // This is the struct (the items of the struct are defined in another file which its header has been included)

       //In the function which runs as thread we have:
         memcpy(&global::Buff_msg[ii], &msg, 1*sizeof( msg_t));

在global1.h

namespace global
{
 extern  msg_t* Buff_msg;
 }

在global1.cpp

#include "global1.h"

namespace global
  {
  msg_t* Buff_msg=(msg_t*) malloc(1000 * sizeof(msg_t));
}

但是,消费者方的global::Buff_msg[ii]全为零。 有人可以帮忙吗?

谢谢。

0 个答案:

没有答案
相关问题