创建多个线程并传参数时,如果传入的参数是个局部变量,最好new内存然后在thread中释放内存,否则这块内存容易被覆盖而导致thread中用的数据发生变化。示例如下:
struct mac_data{
string mac;
string account;
string password;
string status;
};
void function1()
{
vector<mac_data> macs = vector<mac_data>();
/*
code to add items to macs;
*/
for (unsigned int i = 0;i < macs.size();i++)
{
struct mac_data *m = new mac_data();
m->status = macs[i].status;
cout<<"begin to setup machine "<<i <<" "<<m->mac<<". current status is "<<m->status<<endl;
if(m->status !=MACR