gpt4 book ai didi

c++ - 无法专门化函数模板 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept()'

转载 作者:行者123 更新时间:2023-11-28 01:19:51 30 4
gpt4 key购买 nike

<分区>

代码 1:

class thread_obj {
private:
static int instances;
bool run;
static mutex lock;
int threadno;
static map<int, thread> mapOfThreads;
public:
thread_obj():run(true)
{
lock.lock();
threadno = instances++;
thread th(thread_obj::thredfunc, this);
mapOfThreads[threadno] = move(th);

cout << "Thread no is " << threadno << endl;
lock.unlock();
}
static void thredfunc(thread_obj* ptr)
{
while (ptr->run)
{
std::this_thread::sleep_for(100ms);
}
}

void operator()()
{
while (run)
{
std::this_thread::sleep_for(100ms);
}
}

void stop()
{
run = false;
}

static int getTotalthreads()
{
return mapOfThreads.size();
}

~thread_obj()
{
lock.lock();
stop();
if (mapOfThreads[threadno].joinable())
mapOfThreads[threadno].join();
mapOfThreads.erase(threadno);
cout << "Destroyed " << threadno << endl;
lock.unlock();
}
};
int thread_obj::instances = 0;
mutex thread_obj::lock;
map<int, thread> thread_obj::mapOfThreads;

代码 2:

thread_obj():run(true)
{
lock.lock();
threadno = instances++;
thread th(thread_obj(), this);
mapOfThreads[threadno] = move(th);

cout << "Thread no is " << threadno << endl;
lock.unlock();
}

第一个代码工作正常,但像代码 2 中给出的那样更改构造函数会出错。在代码 1 中,构造函数从静态函数创建线程。在代码中,两个构造函数调用非静态 operator()

  1. 'std::invoke': no matching overloaded function found
  2. Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'

这背后的原因是什么?(创建此代码是为了处理多个线程。)

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com