multithreading - Basic Threads using C++ -
this question has answer here:
- start thread member function 4 answers
i trying create simple thread using c++. getting error: error 14 error c2276: '&' : illegal operation on bound member function expression
this code:
void peercommunication(peerdata &peerdata, std::string infohash) { peer peer(peerdata.getip(), peerdata.getport(), infohash); peer.createconnection(); peer.haspeace(0); peer.recievepeace(0, 4056211 + 291); } tcppeers(orderedmap<std::string, unsigned short> peers, std::string infohash, bencoding bencoder) { std::vector<std::thread> threads; //std::thread ttt[num_threads]; //std::thread t1(task1, "hello"); (std::size_t = 0; < peers.getsize(); i++) { peerdata pd(peers.getkeybyindex(i), peers.getvaluebyindex(i)); std::thread t(&peercommunication, pd, infohash); threads.push_back(t); } (std::size_t = 0; < peers.getsize(); i++) { threads.at(i).join(); } ... }
i have tried remove reference: std::thread t(peercommunication, pd, infohash);
, still doesn't works.
when (remove refernce) error is: error 4 error c3867: 'tcppeers::peercommunication': function call missing argument list; use '&tcppeers::peercommunication' create pointer member
you need return peercommunication()
function.
i suspect want little more this:
for (std::size_t = 0; < peers.getsize(); i++) { peerdata pd(peers.getkeybyindex(i), peers.getvaluebyindex(i)); // use emplace std::thread non-copyable // pass pointer member functions // use std::ref pass references threads.emplace_back(&tcppeers::peercommunication, this, std::ref(pd), infohash); } (std::size_t = 0; < peers.getsize(); i++) { threads.at(i).join(); }
Comments
Post a Comment