Why the time displays like that?

(sth wrong about my pc only type in english)

just like the topic
(only want to do a little test about barrier, unexpected problem happens)

source code is:
#include <stdlib.h>
#include <stdio.h>
#include <sync.h>
#include <sys/neutrino.h>

barrier_t barrier; //barrier sysc object


void *
thread1 (void *not_used)
{
time_t now;

time(&now);
printf(“thread1 staring at %s”, ctime(&now));

//do the computation
//lets just do a sleep here
sleep(10);
barrier_wait (&barrier);
//after the point, all 3 threads have completed
time(&now);
printf(“barrier in thread1() done at %s”, ctime(&now));
}

void *
thread2 (void *not_used)
{
time_t now;

time(&now);
printf(“thread2 staring at %s”, ctime(&now));

//do the computation
//lets just do a sleep here
sleep(15);
barrier_wait (&barrier);
//after the point, all 3 threads have completed
time(&now);
printf(“barrier in thread2() done at %s”, ctime(&now));
}

main ()
{
time_t now;

//create a barrier with count of 3
barrier_init(&barrier, NULL,3);

//start up two threads, 1&2
pthread_create(NULL,NULL,thread2,NULL);
pthread_create(NULL,NULL,thread1,NULL);

//at this point, thread1 and thread2 are running

//now wait for completion
time(now);
printf(“main() waiting for barrier at %s”, ctime(&now));
barrier_wait(&barrier);

//after this point, all 3 threads have completed.
time(&now);
printf(“barrier in main() done at %s”, ctime(&now));
}


console shows

thread2 staring at Mon Dec 6 15:14:13 2004
thread1 staring at Thu Jan 1 00:00:00 1970
main() waiting for barrier at Thu Jan 1 00:00:00 1970
barrier in thread2() done at Mon Dec 6 15:14:28 2004
barrier in main() done at Mon Dec 6 15:14:28 2004
barrier in thread1() done at Mon Dec 6 15:14:28 2004

How the ridiculous 1970 comes out? Thanks all so much to give a guidance

another problem: could experts (xtang,wsforever,qnx master etc) tell me the exicution order of avove code? I am not very clear about the order between main and pthread1 & thread2

Thanks all so much! :frowning:

$ qcc -g -o barrier_test barrier_test.c
barrier_test.c: In function main': barrier_test.c:57: warning: passing arg 1 of time’ makes pointer from integer without a cast

上面的这个警告是不是告诉了你什么呢?

关于执行顺序,main先barrier_wait(),thread1在10秒后也barrier_wait(),再过5秒thread2也barrier_wait()从而达到条件。这时,三个线程一起抢CPU,每一个线程都有机会。没有固定的顺序。

实在要说的话,thread2最先执行的机会大一点,因为它是最后获得CPU的。另外,如果main()抢到CPU而先return的话,其它线程很可能没有机会再printf()了。(main()线程退出,整个进程会结束,同进程中的任何线程都会被终止)