一个线程如何终止另一个线程
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
typedef struct arg ARG;
struct arg{
int arg_1;
};
void *f(void *arg){
pthread_t tid;
tid=pthread_self();
int i;
ARG *p=(ARG *)arg;
for(i=0;i<10000000;i++){
if(p->arg_1==i){
printf("%d\n",p->arg_1);
pthread_exit((void*) 1);
//我想此时把线程2也终止了,如果用pthread_cancel的话不知道另外一个线程的id
}else{
return NULL;
}
}
}
int main(){
pthread_t tid_1,tid_2;
ARG myarg_1,myarg_2;
int err;
void *res_1,*res_2;
tid_1=pthread_self();
myarg_1.arg_1=10;
//创建第一个线程
err=pthread_create(&tid_1,NULL,f,(void *)&myarg_1);
if(err!=0){
printf("%s\n",strerror(err));
exit(1);
}
tid_2=pthread_self();
myarg_2.arg_1=1000000;
//创建第二个线程
err=pthread_create(&tid_2,NULL,f,(void *)&myarg_2);
if(err!=0){
printf("%s\n",strerror(err));
exit(1);
}
err=pthread_join(tid_1,&res_1);
err=pthread_join(tid_2,&res_2);
return 0;
}
比如上面的例子,我调用两个线程分别传两个不同的值过去,我想在满足10的时候就终止所有线程,终止该线程本身,我知道,但是终止另外一个线程怎么做。