首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

为什么结果是这样的啊该如何解决

2012-05-24 
为什么结果是这样的啊C/C++ code#includepthread.h#includestdlib.h#includestdio.h#includeerrno.

为什么结果是这样的啊

C/C++ code
#include<pthread.h>#include<stdlib.h>#include<stdio.h>#include<errno.h>pthread_cond_t taxiCond=PTHREAD_COND_INITIALIZER;pthread_mutex_t taxiMutex=PTHREAD_MUTEX_INITIALIZER;void * traveler_arrive(void *name){    int ret;    printf("Traveler %s needs a taxi now1\n",name);    ret=pthread_mutex_lock(&taxiMutex);    if (ret==0) printf("cond is locked\n");    pthread_cond_wait(&taxiCond,&taxiMutex);    printf("test\n");    pthread_mutex_unlock(&taxiMutex);    printf("%s got a taxi\n",name);    printf("traver thread sucess and exit\n");    pthread_exit((void *)0);}void * taxi_arrive(void * name){    int ret;    printf("taxi %s arrive \n",name);    pthread_mutex_unlock(&taxiMutex);    ret=pthread_cond_signal(&taxiCond);    if(ret==0) printf("cond is lived\n");        pthread_mutex_unlock(&taxiMutex);    printf("taxi thread sucess and exit\n");    pthread_exit((void *)0);}void main(){    pthread_t thread;    pthread_attr_t threadAttr;    pthread_attr_init(&threadAttr);    pthread_create(&thread,&threadAttr,taxi_arrive,(void *)("Jack"));    sleep(10);    pthread_create(&thread,&threadAttr,traveler_arrive,(void *)("Susan"));    sleep(10);    pthread_create(&thread,&threadAttr,taxi_arrive,(void *)("Mike"));    sleep(10);    return ;}


输出为:
taxi Jack arrive 
cond is lived
taxi thread sucess and exit
Traveler Susan needs a taxi now1
cond is locked
taxi Mike arrive 
test
Susan got a taxi
traver thread sucess and exit
cond is lived
taxi thread sucess and exit


感觉输出的结果怎么很奇怪啊:怎么 Susan got a taxi在cond is lived
那不就是信号还没出发 而那边的线程就已经得到信号了啊 这是怎么回事啊 她不是应该在等待条件为真的时候才可以将挂起的线程激活吗 怎么结果是这个样子

[解决办法]
信号已经发出去了,然后你才打印的信息。
[解决办法]
对,是这个意思
探讨

你的意思是 打印和发信号在操作时候并不同步 是不

热点排行