中没有类型命名'type'我知道这种类型的问题已经被问到,但我无法弄清我得到的错误的原因..错误:在'class std :: result_of
我想测试锁实现以下使用多线程给出:
class TTAS
{
atomic<bool> state;
public:
TTAS(){
state = ATOMIC_FLAG_INIT;
}
void lock() {
while(true) {
while(state) {};
// using exchange() which is equivalent to getAndSet but with lookup
if (!state.exchange(true)) {
return;
}
}
}
void unlock() {
state.exchange(false);
}
};
我使用下面的代码创建线程此:
void test2(TTAS t) {
}
TTAS t();
for (int i = 0; i < 10; i++) {
// Create a thread and push it into the thread list and call the distributedWrite function
threadList.push_back(std::thread(test2, std::ref(t)));
}
当我编译这段代码我得到下面的错误。我不知道我在这里做错了什么..
In file included from /usr/include/c++/5/thread:39:0,
from assgn4.cpp:17:
/usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_simple<void (*(std::reference_wrapper<TTAS()>))(TTAS)>’:
/usr/include/c++/5/thread:137:59: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(TTAS); _Args = {std::reference_wrapper<TTAS()>}]’
assgn4.cpp:186:60: required from here
/usr/include/c++/5/functional:1505:61: error: no type named ‘type’ in ‘class std::result_of<void (*(std::reference_wrapper<TTAS()>))(TTAS)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/5/functional:1526:9: error: no type named ‘type’ in ‘class std::result_of<void (*(std::reference_wrapper<TTAS()>))(TTAS)>’
_M_invoke(_Index_tuple<_Indices...>)
^
有人可以请解释这个错误。在此先感谢
感谢您的快速响应..我将'原子'改为'原子 *'修复了这个问题.. –
anirudh
@anirudh我的第一步会让'test2'接受一个参考,但我不会不知道你的用例,所以你的解决方案可能是完全有效的,最好的。 – krzaq