(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!