#include <vector>
#include <thread>
#include <stdlib.h>
#include <iostream>
#include <cstdint>
//#define numEntries 100
class CAS
{
private:
std::vector <std::thread> threads;
long tagIndex,arg_tag;
public:
struct vtable
{
long tag;
};
std::vector<vtable> vt;
CAS(int numEntries)
{
vt.resize(numEntries);
}
void pSearch(int i)
{
int a=10;
//unsigned i= *((unsigned *)thInd);
//if(arg_tag==vt[i].tag)
//tagIndex=i;
}
void CAS_threadEND(int i)
{
threads[i].join();
}
void parallelSearchINIT(unsigned numEntries, int _tag)
{
//threads.resize(numEntries);
for(unsigned i=0;i<numEntries;i++)
{
arg_tag=_tag;
threads.push_back(std::thread (CAS::pSearch,i));
// t;
//(void (A::*)(int)) &A::pSearch
}
}
};
int main()
{
int _tag=11;
unsigned numEntries=100;
CAS c(numEntries);
c.parallelSearchINIT(numEntries,_tag);
for(unsigned i=0;i<numEntries;i++)
c.CAS_threadEND(i);
}
我在线程创建行收到以下错误:
suryakalpo@suryakalpo-Lenovo-IdeaPad-Z510:~/Documents/gem5$ g++ - std=c++11 -pthread cas1.cc
cas1.cc: In member function ‘void CAS::parallelSearchINIT(unsigned int, int)’:
cas1.cc:50:48: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, unsigned int&)’
threads.push_back(std::thread (CAS::pSearch,i));
^
cas1.cc:50:48: note: candidates are:
In file included from cas1.cc:2:0:
/usr/include/c++/4.8/thread:133:7: note: std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (CAS::*)(int); _Args = {unsigned int&}]
thread(_Callable&& __f, _Args&&... __args)
^
/usr/include/c++/4.8/thread:133:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘void (CAS::*&&)(int)’
/usr/include/c++/4.8/thread:128:5: note: std::thread::thread(std::thread&&)
thread(thread&& __t) noexcept
^
/usr/include/c++/4.8/thread:128:5: note: candidate expects 1 argument, 2 provided
/usr/include/c++/4.8/thread:122:5: note: std::thread::thread()
thread() noexcept = default;
^
/usr/include/c++/4.8/thread:122:5: note: candidate expects 0 arguments, 2 provided
我尝试通过强制转换成员函数来解决函数重载:threads.push_back(std::thread ((void (CAS::*)(int)) &CAS::pSearch,i));
但是这并没有解决重载问题,而是我得到了这个:
suryakalpo@suryakalpo-Lenovo-IdeaPad-Z510:~/Documents/gem5$ g++ - std=c++11 -pthread cas1.cc
In file included from /usr/include/c++/4.8/thread:39:0,
from cas1.cc:2:
/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (CAS::*)(int)>(unsigned int)>’:
/usr/include/c++/4.8/thread:137:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (CAS::*)(int); _Args = {unsigned int&}]’
cas1.cc:49:70: required from here
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (CAS::*)(int)>(unsigned int)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (CAS::*)(int)>(unsigned int)>’
_M_invoke(_Index_tuple<_Indices...>)
有人可以告诉我为什么会出现功能超载的情况?因为老实说我无法确定超载的来源。此外,是CAS类的成员函数还是类线程的构造函数重载的情况?