C++11之前的<pthread.h>写法
#include<bits/stdc++.h>
using namespace std;
int vall=-1;
void* func1(void*val) {
for(int i=0; i<1000000; i++) {
if(vall!=(int)val) {
printf("Vall changed into %d\n",val);
vall=(int)val;
}
}
pthread_exit(NULL);
return NULL;
}
int main()
{
pthread_t a[11];
for(int i=0; i<10; i++) {
printf("Here is starting %d\n",i);
pthread_create(&a[i],NULL,func1,(void*)i);
}
for(int i=0; i<10; i++) {
pthread_join(a[i],NULL);
}
return 0;
}
C++11的<thread>写法
#include<bits/stdc++.h>
#include<thread>
using namespace std;
int vall=-1;
void* func1(void*val) {
for(int i=0; i<1000000; i++) {
if(vall!=(int)val) {
printf("Vall changed into %d\n",val);
vall=(int)val;
}
}
pthread_exit(NULL);
return NULL;
}
int main()
{
thread a[11];
for(int i=0; i<10; i++) {
printf("Here is starting %d\n",i);
a[i]=thread(func1,(void*)i);
}
for(int i=0; i<10; i++) {
a[i].join();
}
return 0;
}