|
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
pthread_mutex_t mymutex;
pthread_cond_t cond;
void* thread1(void* a){
printf("thread1 running.....\n");
pthread_mutex_lock(&mymutex);
pthread_cond_wait(&cond,&mymutex);
printf("thread1 condition.....\n");
// pthread_cond_destroy(&cond);
pthread_mutex_unlock(&mymutex);
}
void* thread2(void* a){
sleep(2);
printf("thread2 running.....\n");
pthread_mutex_lock(&mymutex);
pthread_cond_wait(&cond,&mymutex);
printf("thread2 condition.....\n");
pthread_cond_destroy(&cond);
pthread_mutex_unlock(&mymutex);
}
int main(){
pthread_t id1,id2;
pthread_mutex_init(&mymutex,NULL);
pthread_cond_init(&cond,NULL);
pthread_create(&id1,NULL,thread1,NULL);
pthread_create(&id2,NULL,thread2,NULL);
pthread_cancel(id1);
sleep(5);
printf("signal\n");
pthread_cond_signal(&cond);
printf("this is out!");
pthread_exit(0);
return 0;
}
这样的代码竟然可以起线程,不知道为什么.不是join后才起动吗.?
还有,pthread_cancel什么意思. |
|