免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1038 | 回复: 0
打印 上一主题 下一主题

多线程里面的pthread_join()参数问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-31 17:08 |只看该作者 |正序浏览

                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(counter2000)
    {
      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(counter2000)
    {
      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;
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/63775/showart_1355895.html
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP