QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 781|回复: 1

linu下线程,条件变量问题.

[复制链接]
发表于 2005-10-22 11:29:52 | 显示全部楼层 |阅读模式
#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什么意思.
发表于 2005-10-23 11:36:10 | 显示全部楼层
join是等待线程结束
pthread_cancel当然是取消线程了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-5 02:04 , Processed in 0.045607 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表