simplified code is like the below, if I just call my_task() in main function, it takes 1s to finish, but if I create another thread to run my_task(), it will take 3.5s to finish. BTW, in my_task(), I am sure that both two methods will do the same amount of loops because I have printed loop times out and compared them. So would anyone tell me why same task in worker thread is much slower than that in main thread? Thanks very much!
void my_task(int id, int param1, My_Type ¶m2){
while(true){
//read some global variables
//modify param2
//generate random int and double
//check if returnable
}
}
int main(){
const int thread_count = 1;
std::thread my_thread = std::thread(my_task, id, param1, std::ref(param2));
my_thread.join();
//my_task(id, param1, param2);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire