从boost lock free spsc队列获取错误输出

Dee*_*ikh 2 queue boost lock-free c++11

我正在尝试使用boost库实现用户定义数据类型的无锁队列,但是我得到了错误的结果.

请帮我解决我做错的地方.

#include <boost/lockfree/spsc_queue.hpp>
#include <thread>
#include <iostream>
#include <string.h>
#include <time.h>   


class Queue
{
 private:
       unsigned char *m_data;
       int m_len;

 public:
       Queue(unsigned char *data,int len);

       Queue(const Queue &obj);

       ~Queue();  

       Queue & operator =(const Queue &obj);


       unsigned char *getdata()
       {
         return m_data;
       }

       int getint()
       {
           return m_len;
       }
};

Queue::Queue(unsigned char* data, int len)
{
      m_len=len;

      m_data=new unsigned char[m_len];

      memcpy(m_data,data,m_len);
}

Queue::Queue(const Queue& obj)
{
      m_len=  obj.m_len;

      m_data=new unsigned char[m_len];

      memcpy(m_data,(unsigned char *)obj.m_data,m_len);
}

Queue::~Queue()
{
   delete[] m_data;
   m_len=0;
}

Queue & Queue::operator =(const Queue &obj)
{
   if(this != &obj)
   {
      m_len=obj.m_len;

      m_data=new unsigned char[m_len];

      memcpy(m_data,(unsigned char *)obj.m_data,m_len);
   }

   return *this;
}

boost::lockfree::spsc_queue<Queue*> q(10);



void produce()
{
    int i=0;
    unsigned char* data=(unsigned char *)malloc(10);
    memset(data,1,9);

    Queue obj(data,10);

    Queue *pqueue=&obj;

    printf("%d\n",pqueue->getint());


    q.push(pqueue);

}

void consume()
{

    Queue *obj;

    q.pop(&obj);

    printf("%d\n",obj->getint());

}


int main(int argc, char** argv) {


//  std::thread t1{produce};
//  std::thread t2{consume};
//  
//  t1.join();
//  t2.join();
    produce();
    consume();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

根据我在课堂上创建的boost :: lockfree :: queue要求.

  • 复制构造函数
  • 分配操作员
  • 析构函数

如果有其他要求,请告诉我.谢谢.

seh*_*ehe 15

  1. malloc在C++中使用.

    你完蛋了.

    你还有2条生命.

    说真的,不要那样做.特别是因为使用它delete[]是明确的未定义行为.


  2. 可悲的是你在这里失去了另一种生活

    Queue obj(data,10);
    
    Queue *pqueue=&obj;
    q.push(pqueue);
    
    Run Code Online (Sandbox Code Playgroud)

    您存储指向本地的指针.更多未定义的行为

    你剩下1点生命.


  3. 上次生命在

    q.pop(&obj);
    
    Run Code Online (Sandbox Code Playgroud)

    你使用迭代器弹出.它将被视为输出迭代器.你得到回报,表示弹出元素的数量,以及项目将被写入&obj[0],&obj[1],&obj[2],等.

    你猜怎么着?未定义的行为.

    另请参见:Boost spsc queue segfault

    你死了.


  4. 你已经死了 但你放弃了你的来世

    printf("%d\n",obj->getint());
    
    Run Code Online (Sandbox Code Playgroud)

    由于pop可能没有弹出任何东西(队列可能是空的),这本身就是未定义的行为.


有趣的是,你谈论所有这些构造函数的要求,你将指针存储在lockfree队列中......?!写吧:

typedef std::vector<unsigned char> Data;

class Queue {
    private:
    Data m_data;

    public:
    Queue(Data data) : m_data(std::move(data)) {}
    Queue() : m_data() {}
    unsigned char const *getdata() const { return m_data.data(); } 
    size_t               getint()  const { return m_data.size(); } 
};

boost::lockfree::spsc_queue<Queue> q(10);
Run Code Online (Sandbox Code Playgroud)

Live On Coliru

笔记:

  • 需要让消费者检查返回代码pop.推送可能没有发生,并且无锁队列不会阻塞.

  • 你不需要这个装置.只需传递矢量:

C++代码

Live On Coliru

#include <boost/lockfree/spsc_queue.hpp>
#include <thread>
#include <iostream>
#include <vector>

typedef std::vector<unsigned char> Queue;

boost::lockfree::spsc_queue<Queue> q(10);

void produce() {
    Queue obj(10, 1);

    std::cout << __FUNCTION__ << " - " << obj.size() << "\n";

    q.push(std::move(obj));
}

void consume() {
    Queue obj;
    while (!q.pop(obj)) { }

    std::cout << __FUNCTION__ << " - " << obj.size() << "\n";
}

int main() {
    std::thread t1 {produce};
    std::thread t2 {consume};

    t1.join();
    t2.join();
}
Run Code Online (Sandbox Code Playgroud)

  • 我已经把UB的清单做得更完整了.对不起,这听起来有点像抨击.但是**如果你不安全的话,**真的不要使用C++.这个世界是一个岌岌可危的地方. (3认同)