call_once выдает исключение с кодом ошибки -1 - PullRequest
3 голосов
/ 05 августа 2020

результат кода ниже: Unknown error -1.

widget.h:

#ifndef WIDGET_H_
#define WIDGET_H_

#include <memory>

class widget
{
public:
    static widget& get_instance();

private:
    static std::unique_ptr<widget> instance;
};

#endif

widget. cpp:

#include <mutex>
#include <thread>
#include "class.h"

std::unique_ptr<widget> widget::instance;
std::once_flag is_create;

widget& widget::get_instance()
{
    std::call_once(is_create, [=]{ instance = std::make_unique<widget>(); });
    return *instance;
}

main. cpp:

#include "widget.h"
#include <iostream>
#include <exception>

int main(int argc, char* argv[]) 
{
    try
    {
        widget::get_instance();
    }
    catch (std::system_error e)
    {
        std::cout << e.what() << std::endl;
    }

    return 0;
}

this говорит, что

std :: system_error, если какое-либо условие предотвращает выполнение вызовов call_once, как указано

Это означает, что make_unique не удалось?

Я подключаюсь к gdb, и мне кажется, что что-то не так в строке if (__gthread_active_p ()) и __gthread_active_ptr == 0. Я не могу понять, что

почему будет сгенерировано это исключение.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...