Focus : Note the use of the fourth parameter of Pthread_create ()
Here are the classic routines (source: Csdn Ginger):
#include <iostream>#include<pthread.h>using namespacestd;#defineNum_threads 10void* Say_hello (void*args) { inti = * ((int*) args);//casts a cast of the passed arguments, which are changed from the untyped pointer to the number of shaped pointers, and then read;cout <<"Hello in"<< I <<Endl;}intMain () {pthread_t tids[num_threads]; cout<<"Hello in main ..."<<Endl; for(inti =0; i < num_threads; ++i) {intret = Pthread_create (&tids[i], NULL, Say_hello, (void*) &i);//the input must be cast to the void* type, that is, the untyped pointercout <<"Current pthread id ="<< Tids[i] << Endl;//this learns the process ID information created using the TIDs array print; if(Ret! =0) {cout<<"pthread_create error:error_code="<< ret <<Endl; }} pthread_exit (NULL);}
Multithreaded programming: Incoming parameters when a thread calls a function