I am trying a simple thread program on using gcc HEAD 7.0.0 20161020 and c+1z(GNU)
Very simple program,
#include <thread>
#include <iostream>
class Task
{
public:
Task(int i):_i(i){}
Task(const Task& p):_i(p._i){std::cout<<"Copy called:"<<_i<<std::endl;}
void operator()(){std::cout<<"operator()"<<_i<<std::endl;}
void runTask(void* p)
{
if (p) {}
std::cout<<"runTask():"<<_i<<std::endl;
}
private:
int _i;
};
int main()
{
std::cout<<"Begin main()"<<std::endl;
Task tsk1(11);
Task tsk2(22);
Task tsk3(33);
std::cout<<"Create threads"<<std::endl;
std::thread t1 {tsk1};
std::thread t2(&Task::runTask, &tsk2, nullptr);
std::thread t3(&Task::runTask, &tsk3, nullptr);
t1.join();
t2.join();
t3.join();
std::cout<<"End main()"<<std::endl;
return 0;
}
Which gives output as:-
Begin main()
Create threads
Copy called:11
Copy called:11
operator()11
runTask():22
runTask():33
End main()
0
Can anyone suggest why Copy is created for single object whereas I am passing 2 different this pointers ?
Aucun commentaire:
Enregistrer un commentaire