barrier question

Hi all,
I run a barrier program demo and
thread1 does not run printf(…) after pthread_wait().


Here is the source ==>

#include <stdio.h>
#include <time.h>
#include <sync.h>
#include <sys/neutrino.h>

barrier_t barrier;

void* thread1(void *un)
{
time_t now;
time(&now);
printf(“thread1 start at time %s\n”, ctime(&now));
sleep(3);
pthread_barrier_wait(&barrier);
time(&now);
printf(“thread1 done at %s\n”, ctime(&now));
fflush(stdout);
}

void* thread2(void *un)
{
time_t now;
time(&now);
printf(“thread2 start at time %s\n”, ctime(&now));
sleep(6);
pthread_barrier_wait(&barrier);
time(&now);
printf(“thread2 done at %s\n”, ctime(&now));

}


main(int argc, char **argv)
{
time_t now;
pthread_barrier_init(&barrier, NULL, 3);

pthread_create(NULL, NULL, thread1, NULL);
pthread_create(NULL, NULL, thread2, NULL);
time(&now);
printf(“main waiting for barrier at %s\n”, ctime(&now));
pthread_barrier_wait(&barrier);
time(&now);
printf(“barrier in main done at %s\n”, ctime(&now));



and this is what i see ==>

\

./barrier_demo

thread1 start at time Fri May 6 16:36:14 2005

main waiting for barrier at Fri May 6 16:36:14 2005

thread2 start at time Fri May 6 16:36:14 2005

thread2 done at Fri May 6 16:36:20 2005

barrier in main done at Fri May 6 16:36:20 2005

No notification by thread1 appears after phtread_barrier_wait in thread1
Any ideas about why is this happening?

Best Regards
george

Is this because both threads are running SCHED_FIFO at same priority? After
thread2 calls barrier, it continues to progress, and exits eventually,
before thread1 gets a chance to exit. Once thread2 exits, the main program
exits as well.

Vilas

<gxen@unipi.gr> wrote in message news:d5g679$f4b$1@inn.qnx.com

Hi all,
I run a barrier program demo and
thread1 does not run printf(…) after pthread_wait().


Here is the source ==

#include <stdio.h
#include <time.h
#include <sync.h
#include <sys/neutrino.h

barrier_t barrier;

void* thread1(void *un)
{
time_t now;
time(&now);
printf(“thread1 start at time %s\n”, ctime(&now));
sleep(3);
pthread_barrier_wait(&barrier);
time(&now);
printf(“thread1 done at %s\n”, ctime(&now));
fflush(stdout);
}

void* thread2(void *un)
{
time_t now;
time(&now);
printf(“thread2 start at time %s\n”, ctime(&now));
sleep(6);
pthread_barrier_wait(&barrier);
time(&now);
printf(“thread2 done at %s\n”, ctime(&now));

}


main(int argc, char **argv)
{
time_t now;
pthread_barrier_init(&barrier, NULL, 3);

pthread_create(NULL, NULL, thread1, NULL);
pthread_create(NULL, NULL, thread2, NULL);
time(&now);
printf(“main waiting for barrier at %s\n”, ctime(&now));
pthread_barrier_wait(&barrier);
time(&now);
printf(“barrier in main done at %s\n”, ctime(&now));



and this is what i see ==

\

./barrier_demo

thread1 start at time Fri May 6 16:36:14 2005

main waiting for barrier at Fri May 6 16:36:14 2005

thread2 start at time Fri May 6 16:36:14 2005

thread2 done at Fri May 6 16:36:20 2005

barrier in main done at Fri May 6 16:36:20 2005

No notification by thread1 appears after phtread_barrier_wait in thread1
Any ideas about why is this happening?

Best Regards
george

gxen@unipi.gr wrote:

Hi all,
I run a barrier program demo and
thread1 does not run printf(…) after pthread_wait().

This does not appear to be complete – in particular, main()
does not appear to be complete. Could you post the entire
code?

-David


Here is the source ==

#include <stdio.h
#include <time.h
#include <sync.h
#include <sys/neutrino.h

barrier_t barrier;

void* thread1(void *un)
{
time_t now;
time(&now);
printf(“thread1 start at time %s\n”, ctime(&now));
sleep(3);
pthread_barrier_wait(&barrier);
time(&now);
printf(“thread1 done at %s\n”, ctime(&now));
fflush(stdout);
}

void* thread2(void *un)
{
time_t now;
time(&now);
printf(“thread2 start at time %s\n”, ctime(&now));
sleep(6);
pthread_barrier_wait(&barrier);
time(&now);
printf(“thread2 done at %s\n”, ctime(&now));

}



main(int argc, char **argv)
{
time_t now;
pthread_barrier_init(&barrier, NULL, 3);

pthread_create(NULL, NULL, thread1, NULL);
pthread_create(NULL, NULL, thread2, NULL);
time(&now);
printf(“main waiting for barrier at %s\n”, ctime(&now));
pthread_barrier_wait(&barrier);
time(&now);
printf(“barrier in main done at %s\n”, ctime(&now));



and this is what i see ==


./barrier_demo

thread1 start at time Fri May 6 16:36:14 2005

main waiting for barrier at Fri May 6 16:36:14 2005

thread2 start at time Fri May 6 16:36:14 2005

thread2 done at Fri May 6 16:36:20 2005

barrier in main done at Fri May 6 16:36:20 2005

No notification by thread1 appears after phtread_barrier_wait in thread1
Any ideas about why is this happening?

Best Regards
george


David Gibbs
QNX Training Services
dagibbs@qnx.com