int *ret;
pthread_join(tid1,(void*)&ret);
if((int)ret==0)
printf("thread1------------------------exit!");
正常。
为何:
pthread_join(tid1,(void*)&ret);
if(*ret==0)
printf("thread1------------------------exit!");
就segment fault ,程序不执行了。
//一个线程去加数,另一个去减。
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
void maketimeout(struct timespec *tp,int minutes)
{
struct timeval now;
gettimeofday(&now,NULL);
tp->tv_sec=now.tv_sec;
tp->tv_nsec=now.tv_usec*1000;
tp->tv_sec+=minutes*60;
}
pthread_cond_t cond_counter=PTHREAD_COND_INITIALIZER;
pthread_mutex_t plock=PTHREAD_MUTEX_INITIALIZER;
unsigned counter=0;
/*条件变量只是起阻塞和唤醒线程的作用,具体的判断条件还需用户给出,例如一个变量是否为0等等,这一点我们从后面的例子中可以看到。
线程被唤醒后,它将重新检查判断条件是否满足,如果还不满足,一般说来线程应该仍阻塞在这里,被等待被下一次唤醒。这个过程一般用
while语句实现。*/
void decrement_counter(void)
{
pthread_mutex_lock(&plock);
while(counter==0)
{
printf("waiting for adding!");
pthread_cond_wait(&cond_counter,&plock);
}
counter--;
pthread_mutex_unlock(&plock);
}
void add_counter(void)
{
pthread_mutex_lock(&plock);
counter++;
if(counter==1)
{
pthread_cond_signal(&cond_counter);
}
pthread_mutex_unlock(&plock);
}
void *thread_add(void *arg)
{
while(counter<2000)
{
add_counter();
//sleep(1);
printf("ADD:---%u\n",counter);
}
pthread_exit((void*)0);
}
void *thread_decrement( void *arg)
{
while(1)
{
decrement_counter();
sleep(1);
printf("SUB:---%u\n",counter);
}
pthread_exit((void *)0);
}
int main()
{
struct timespec time_out;
maketimeout(&time_out,3);
printf("the time out seconds is %d\n",time_out.tv_sec);
//thread variable
pthread_t tid1,tid2;
int err;
int *ret;
// thread1;
err=pthread_create(&tid1,NULL,thread_add,NULL);
if(err)
printf("Can not create thread:%s", strerror(err));
//thread2
err=pthread_create(&tid2,NULL,thread_decrement,NULL);
if(err)
printf("Can not create thread:%s", strerror(err));
pthread_join(tid1,(void*)&ret);
if((int)ret==0)
printf("thread1------------------------exit!");
pthread_join(tid2,NULL);
printf("main---------------------------------exit!\n");
return 0;
}
|
加入一个时间参数:用pthread_cond_timedwait()函数,
加的要65秒后加第二次,而thread_decrement等待的时间设为60秒。
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
void maketimeout(struct timespec *tp,int minutes)
{
struct timeval now;
gettimeofday(&now,NULL);
tp->tv_sec=now.tv_sec;
tp->tv_nsec=now.tv_usec*1000;
tp->tv_sec+=minutes*60;
}
pthread_cond_t cond_counter=PTHREAD_COND_INITIALIZER;
pthread_mutex_t plock=PTHREAD_MUTEX_INITIALIZER;
unsigned counter=0;
/*条件变量只是起阻塞和唤醒线程的作用,具体的判断条件还需用户给出,例如一个变量是否为0等等,这一点我们从后面的例子中可以看到。
线程被唤醒后,它将重新检查判断条件是否满足,如果还不满足,一般说来线程应该仍阻塞在这里,被等待被下一次唤醒。这个过程一般用
while语句实现。*/
void decrement_counter(void)
{
struct timespec time_wait;
int err;
maketimeout(&time_wait,1);;
pthread_mutex_lock(&plock);
while(counter==0)
{
printf("waiting for adding!\n");
//pthread_cond_wait(&cond_counter,&plock);
err=pthread_cond_timedwait(&cond_counter,&plock,&time_wait);
if(err==ETIMEDOUT)
{
printf("time out!\n");
break;
}
}
counter--;
pthread_mutex_unlock(&plock);
}
void add_counter(void)
{
pthread_mutex_lock(&plock);
counter++;
if(counter==1)
{
pthread_cond_signal(&cond_counter);
}
pthread_mutex_unlock(&plock);
}
void *thread_add(void *arg)
{
while(counter<2000)
{
add_counter();
sleep(65);
printf("ADD:---%u\n",counter);
}
pthread_exit((void*)0);
}
void *thread_decrement( void *arg)
{
while(1)
{
decrement_counter();
sleep(1);
printf("SUB:---%u\n",counter);
}
pthread_exit((void *)0);
}
int main()
{
struct timespec time_out;
maketimeout(&time_out,3);
printf("the time out seconds is %d\n",time_out.tv_sec);
//thread variable
pthread_t tid1,tid2;
int err;
int *ret;
// thread1;
err=pthread_create(&tid1,NULL,thread_add,NULL);
if(err)
printf("Can not create thread:%s", strerror(err));
//thread2
err=pthread_create(&tid2,NULL,thread_decrement,NULL);
if(err)
printf("Can not create thread:%s", strerror(err));
pthread_join(tid1,(void*)&ret);
if((int)ret==0)
printf("thread1------------------------exit!");
pthread_join(tid2,NULL);
printf("main---------------------------------exit!\n");
return 0;
}
|
阅读(2805) | 评论(0) | 转发(0) |