C++ 尝试实现多线程队列时_M_构造空无效错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40677305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:24:23  来源:igfitidea点击:

_M_ construct null not valid error when trying to implement multi threaded queue

c++multithreading

提问by anirudh

I am trying to implement a priority queue using using a simple linear approach as explained in Art of Multiprocessor programming. I'm new to c++ and have difficulty troubleshooting.

我正在尝试使用多处理器编程艺术中解释的简单线性方法来实现优先级队列。我是 C++ 新手,很难排除故障。

I've implemented two template classes and am testing them using a simple test method. As I'm not able to pin point the error, I'm pasting all the three classes below for reference.

我已经实现了两个模板类,并且正在使用一种简单的测试方法来测试它们。由于我无法指出错误,我将粘贴以下所有三个类以供参考。

I know that _M_ construct null not validcomes when trying to construct a string using nullptr, but am not sure where I'm doing that.

我知道_M_ construct null not valid尝试使用 构造字符串时会出现这种情况nullptr,但不确定我在哪里这样做。

The three classes created are given below:

创建的三个类如下:

bin.h

二进制文件

#include <mutex>
#include <deque>
#include <memory>
#include <iostream>
using namespace std;

namespace priority
{
    template<typename T>
    class Bin
    {
    private:
        std::deque<T> v;
        std::mutex m;
    public:
        Bin() {
        }

        Bin(const Bin &o) {

        }

        const Bin &operator=(const Bin &other) {
            return *this;
        }

        void put(T item) {
            std::lock_guard<std::mutex> lock(m);
            v.push_back(item);
        }

        T *get() {
            std::lock_guard<std::mutex> lock(m);
            if (v.size() == 0) {
                return nullptr;
            }
            else {
                T val = v.front();
                T *ptr_val = &(val);
                v.pop_front();
                return ptr_val;
            }
        }

        bool isEmpty() {
            std::lock_guard<std::mutex> lock(m);
            return v.size() == 0;
        }

    };  
}

SimpleLinear.h

简单线性.h

#include <mutex>
#include <vector>
#include <memory>
#include "Bin.h"

namespace priority
{   
    template<typename T>
    class SimpleLinear
    {
    private:
        int range;
        std::vector<Bin<T>> pqueue;
    public:
        SimpleLinear(int range){
            this->range = range;

            for (int i = 0; i < range; i++)
            {
                pqueue.push_back(Bin<T>());
            }

        }

        void add(T item, int key) {
            pqueue[key].put(item);
        }

        T removeMin() {
            for (int i = 0; i < range; i++)
            {
                T *item = pqueue[i].get();
                if (item != nullptr) {
                    return *item;
                }
            }
            return nullptr;
        }

    };
}

test.cpp

测试.cpp

#include <iostream>
#include <vector>
#include <thread>
#include <algorithm>
#include "SimpleLinear.h"
using namespace std;
using namespace priority;

void te(SimpleLinear<string> s, int thread_id) {

    s.add("sundar"+to_string(thread_id), thread_id);
    s.add("akshaya"+to_string(thread_id), 3);
    s.add("anirudh"+to_string(thread_id), 1);
    s.add("aaditya"+to_string(thread_id), 5);
    cout << s.removeMin() << endl;
    cout << s.removeMin() << endl;
    cout << s.removeMin() << endl;

}


int main(int argc, char const *argv[])
{   

    SimpleLinear<string> s(100);
    std::vector<std::thread> v;

    for (int i = 0; i < 100; i++)
    {   
        // if (i % 2 == 0)
            v.push_back(thread(te, std::ref(s), i));
        // else
            // v.push_back(thread(t, std::ref(s), i));


    }
    for_each(v.begin(), v.end(), std::mem_fn(&std::thread::join));
    return 0;
}

I'm getting the error:

我收到错误:

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid
terminate called recursively
terminate called recursively
terminate called recursively
Aborted (core dumped)

回答by Maxim Egorushkin

One reason it crashes is that in SimpleLinear<T>::removeMinwhen Tis std::string, return nullptrconstructs a string from nullptr. basic_string::_M_construct null not validbasically says that std::string(nullptr)was invoked.

它崩溃的一个原因是在SimpleLinear<T>::removeMinwhen Tis 中std::stringreturn nullptrnullptr. basic_string::_M_construct null not valid基本上说std::string(nullptr)被调用了。



Another reason it may crash is that getfunction returns a pointer to a local variable. Local variables get destroyed when the function returns. This leads to undefined behavior.

它可能崩溃的另一个原因是get函数返回一个指向局部变量的指针。当函数返回时,局部变量会被销毁。这会导致未定义的行为。

A fix:

一个修复:

bool get(T& result) {
    std::lock_guard<std::mutex> lock(m);
    if (v.empty()) 
        return false;
    result = v.front();
    v.pop_front();
    return true;
}

And invoke it like:

并像这样调用它:

T removeMin() {
    T result;
    for(int i = 0; i < range; i++)
        if(pqueue[i].get(result))    
            break;
    return result;
}