@Bratis4ka

Как правильно создать поток thread с методом класса в c++?

Не могу понять как мне создать поток.

Часть кода:
#include <thread>

std::shared_ptr<Admin::ServerManager> Admin::ServerManager::mServerManager = nullptr;
std::mutex Admin::ServerManager::mMutex{};

bool Admin::ServerManager::exec(commandType type)
{
    std::thread th(&ServerManager::start);
    switch (type)
    {
        case START:
            start();
            return true;

        case STOP:
            stop();
            return true;
//Some Code

void Admin::ServerManager::start()
{
    if(serverStatus == 0)
    {
        serverStatus = 1;
        Admin::ServerManager::Socket.reset(new Network::ServerSocket);
    }
    else
        std::cout << "The server is already up";
}


Ошибки, которые вылетают при билде:
1) /usr/include/c++/9/thread:120: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues
In file included from /home/left/Desktop/Projects/SocketServer/src/Core/AdminComponent/src/ServerManager.cpp:2:
/usr/include/c++/9/thread: In instantiation of ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Admin::ServerManager::*)(); _Args = {}; = void]’:
/home/left/Desktop/Projects/SocketServer/src/Core/AdminComponent/src/ServerManager.cpp:9:41: required from here
/usr/include/c++/9/thread:120:44: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues
120 | typename decay<_Args>::type...>::value,
| ^~~~~
2)/usr/include/c++/9/thread:243: error: no type named ‘type’ in ‘struct std::thread::_Invoker >::__result >’
/usr/include/c++/9/thread:131:22: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Admin::ServerManager::*)(); _Args = {}; = void]’
/home/left/Desktop/Projects/SocketServer/src/Core/AdminComponent/src/ServerManager.cpp:9:41: required from here
/usr/include/c++/9/thread:243:4: error: no type named ‘type’ in ‘struct std::thread::_Invoker >::__result >’
243 | _M_invoke(_Index_tuple<_Ind...>)
| ^~~~~~~~~
3)/usr/include/c++/9/thread:247: error: no type named ‘type’ in ‘struct std::thread::_Invoker >::__result >’
/usr/include/c++/9/thread:247:2: error: no type named ‘type’ in ‘struct std::thread::_Invoker >::__result >’
247 | operator()()
| ^~~~~~~~
  • Вопрос задан
  • 48 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы