Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 15 Jan 1998 16:19:55 +0200 (SAT)
From:      Jacques Fourie <jacques@oskar.nanoteq.co.za>
To:        freebsd-hackers@FreeBSD.ORG
Subject:   Pthreads question
Message-ID:  <199801151419.QAA24608@oskar.nanoteq.co.za>

next in thread | raw e-mail | index | archive | help
Hi

I'm running FreeBSD2.2.5-RELEASE and I'm noticing weird behaviour when using
the threaded version of libc, libc_r. Included is an example program. When
started with parameter 1, the pthread_cond_timedwait() function returns an
error with errno=0. When started with parameter 0 everything seems to be
working fine. The only difference between the 2 arguments is that in the
first case the pthread_cond_timedwait() is not called in the main thread.

Any help will be appreciated.
Jacques Fourie

-------------------Source code-----------------------------------------

/* Compiled with gcc -o threadtest threadtest.c -lc_r */
#include <pthread.h>
#include <stdio.h>
#include <errno.h>

pthread_t th;
pthread_t th2;
pthread_t th3;
pthread_cond_t co;
pthread_mutex_t mu;
int fin=0;

void fg(void)
{
  struct timespec ts;
  struct timeval tv;

  pthread_mutex_lock(&mu);
  while (!fin) {
    gettimeofday(&tv,NULL);
    TIMEVAL_TO_TIMESPEC(&tv,&ts);
    ts.tv_sec += 1;
    printf("[.]\n");   
    if (pthread_cond_timedwait(&co,&mu,&ts) == -1) {
      if (errno != EAGAIN) {
        printf("timedwait error: %d\n",errno);
        break;
      }
    }
  }
  pthread_mutex_unlock(&mu);
}

void bg(void)
{
  sleep(2);
  pthread_mutex_lock(&mu);
  fin=1;
  pthread_mutex_unlock(&mu);
  pthread_cond_signal(&co);
}

int main(int argc,char * argv[])
{
  if (argc<2)
    exit(1);

  pthread_mutex_init(&mu,pthread_mutexattr_default);
  pthread_cond_init(&co,pthread_condattr_default);

  pthread_create(&th,pthread_attr_default,(void *)bg,NULL);

  if (argv[1][0]=='1')
    pthread_create(&th2,pthread_attr_default,(void *)fg,NULL);
  else
    fg();

  pthread_join(th,NULL);
  pthread_detach(&th);
  pthread_cond_destroy(&co);
  pthread_mutex_destroy(&mu);

  return 0;
}
---------------------------------Snip-----------------------------------------




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199801151419.QAA24608>