c++11 || std::thread i std::thread

Permalänk
Medlem

c++11 || std::thread i std::thread

Hej.

Mitt errror jag har i min riktga klass är i still med t3 anropet antar det har något att göra med parametrarna. Men jag förstår inte vad.

Då det hade blivit lite långt med min klass så kommer följande exemple kod

#include <iostream> #include <thread> using namespace std; class Fctor { public: void operator() (string msg) { std :: cout << "From Fctor: "<< msg << std :: endl; std::thread t2(&Fctor::test, this); t2.join(); } private: void test(){ std::cout << "yafoaf" << std::endl; std::thread t3(&Fctor::test2, 42, 14, this); } void test2(int sd, int i){ std::cout << "Tja lalalala" << std::endl; std::cout << "sd: " << sd << " i: " << i << std::endl; } }; int main() { string s = "Hello world"; std::thread t1((Fctor()),s); t1.join(); }

Om någon kan förklara vad jag gör fel hade det uppskattats. Är det så att en std::thread bara kan hantera viss mängd parametrar? eller är det för att det är den en thread i en thread med parametrar som gör det?

Jag förstår inte riktig.

Tack för svaR!

Visa signatur

Смерть -это решение всех проблем. Нет человека - нет проблемы
Comp1: Ubuntu 16.04 Comp2: Arch Linux
Comp3: Ubuntu Server 16.04 Comp4: Centos 6.5
Comp5: Linux mint 16 Comp6: Raspberry pi (olika OS hela tiden)
Phone: Motorola Google Nexus 6

Permalänk
Medlem

Ska kanske lägga till erroret (riktiga klassen)

In file included from /usr/include/c++/5/memory:79:0, from ServerSocketHandler.h:24, from ServerSocketHandler.cpp:7: /usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (ServerSocketHandler::*)(int, short int)>(int, short int, ServerSocketHandler*)>’: /usr/include/c++/5/thread:137:59: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (ServerSocketHandler::*)(int, short int); _Args = {int&, short int&, ServerSocketHandler*}]’ ServerSocketHandler.cpp:276:68: required from here /usr/include/c++/5/functional:1505:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (ServerSocketHandler::*)(int, short int)>(int, short int, ServerSocketHandler*)>’ 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<std::_Mem_fn<void (ServerSocketHandler::*)(int, short int)>(int, short int, ServerSocketHandler*)>’ _M_invoke(_Index_tuple<_Indices...>) ^ Makefile:54: recipe for target 'compile' failed make: *** [compile] Error 1

test klassen

In file included from /usr/include/c++/5/thread:39:0, from tmp.cpp:2: /usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (Fctor::*)(int)>(int, Fctor*)>’: /usr/include/c++/5/thread:137:59: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Fctor::*)(int); _Args = {int, Fctor*}]’ tmp.cpp:15:49: required from here /usr/include/c++/5/functional:1505:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (Fctor::*)(int)>(int, Fctor*)>’ 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<std::_Mem_fn<void (Fctor::*)(int)>(int, Fctor*)>’ _M_invoke(_Index_tuple<_Indices...>) ^

Visa signatur

Смерть -это решение всех проблем. Нет человека - нет проблемы
Comp1: Ubuntu 16.04 Comp2: Arch Linux
Comp3: Ubuntu Server 16.04 Comp4: Centos 6.5
Comp5: Linux mint 16 Comp6: Raspberry pi (olika OS hela tiden)
Phone: Motorola Google Nexus 6

Permalänk
Legendarisk

@asdfgh: Objektet metoden ska anropas på behöver vara det andra argumentet till threads konstruktor:
std::thread t3(&Fctor::test2, this, 42, 14);

Sedan behöver du vänta in t3.

Visa signatur

Abstractions all the way down.

Permalänk
Medlem

Jo, t3 måste jag lägga join på, men ja det var vad mitt problem var.

Visa signatur

Смерть -это решение всех проблем. Нет человека - нет проблемы
Comp1: Ubuntu 16.04 Comp2: Arch Linux
Comp3: Ubuntu Server 16.04 Comp4: Centos 6.5
Comp5: Linux mint 16 Comp6: Raspberry pi (olika OS hela tiden)
Phone: Motorola Google Nexus 6