中沒有類型命名'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