class Thread
{
Sync::TYPE sync; // synchronous start/finish
char name[256]; // thread name
thread handle; // thread handle
HANDLE abort, // abort event handle
start; // start event handle
public:
Thread(const char *desc,Sync::TYPE ctrl,void (*fn)(void *),void *arg=NULL);
Thread(Thread &th);
Thread(void);
~Thread(void);
HANDLE AbortHandle(void) const { return(abort); }
HANDLE StartHandle(void) const { return(start); }
thread::id ID(void) const { return(handle.get_id()); }
void Name(const char *value);
const char *Name(void) const { return(name); }
void Sync(Sync::TYPE value) { sync=value; }
Sync::TYPE Sync(void) const { return(sync); }
thread *ThreadHandle(void) { return(&handle); }
Thread &operator=(Thread &th);
};
class ThreadList
{
map<thread::id,Thread> TiL;
public:
ThreadList(void);
~ThreadList(void);
HANDLE AbortHandle(void);
void Clear(void);
bool Close(thread::id id);
void Close(map<thread::id,Thread>::iterator pos);
map<thread::id,Thread>::iterator Create(char *name,Sync::TYPE sync,void (*function)(void*),void *arg=NULL);
map<thread::id,Thread>::iterator Find(thread::id id) { return(TiL.find(id)); }
int Size(void) const { return(TiL.size()); }
void Start(map<thread::id,Thread>::iterator pos);
HANDLE StartHandle(void);
};
现在此代码生成错误:
map<thread::id,Thread>::iterator ThreadList::Create(char *name,Sync::TYPE sync,void (*function)(void*),void *arg)
{
pair<map<thread::id,Thread>::iterator,bool> result;
thread::id id;
Thread th(name,sync,function,arg);
id=th.ThreadHandle()->get_id();
**result=TiL.insert(make_pair(id,th));**
return(result.first);
}
错误的行以粗体显示。确切的错误信息是:
错误C2664:'std :: _ Tree_iterator&gt;&gt; std :: _ Tree&gt; :: insert(std :: _ Tree_const_iterator&gt;&gt;,const std :: pair&amp;)':无法将参数1从'std :: pair'转换为'std :: pair&amp;&amp;'< / p>
我在这里做错了什么?
亲切的问候,瓦迪姆。
P.S。:对不起,我应该澄清那一刻。也有这些定义:
Thread::Thread(Thread &th)
{
sync=th.sync;
StrCopy(name,sizeof(name),th.name);
abort=th.abort;
start=th.start;
handle=move(th.handle);
}
Thread &Thread::operator=(Thread &th)
{
sync=th.sync;
StrCopy(name,sizeof(name),th.name);
abort=th.abort;
start=th.start;
handle=move(th.handle);
return(*this);
}
答案 0 :(得分:1)
如果没有MCVE,很难肯定地说,但是看看你的例子,我看到thread handle
课程中最明显的是Thread
。我假设这是一个std::thread,如果是这样,std :: thread状态的文档
没有两个std :: thread对象可能代表相同的执行线程; std :: thread不是CopyConstructible或CopyAssignable,尽管它是 MoveConstructible和MoveAssignable。
要修复它,你必须实现移动操作符以确保你的Thread类可以放入容器中。
然后,您就可以std::move(th)
进入地图容器,或者直接使用map.emplace
构建
我建议阅读有关何时以及如何实施复制/移动的文章,因为很容易弄错这些错误,默认情况可能还不错。