It is currently Sun, 16 Jun 2019 00:36:40 GMT



 
Author Message
 Strange pthreads problem on Solaris

I have compiled the following test program on Solaris and I see some
strange behaviour.  I have simplified this program so that there is only
one thread that waits on a condition variable.  The other thread just
periodically dumps some output to stdout.

The example below runs successfully on my machine, but if I change the
if (threadno == 0) statement to "if (threadno == 1)", the behaviour
changes.  Why!?

I compiled this program on Solaris 2.5.1 using:

        CC -mt cvtest.cc -o cvtest

Thanks,
Ben

-- cut here --

/*
 * This program demonstrates a problem in cancelling threads where
 * one thread is waiting on a condition variable.
 *
 * pthread_cancel always seems to work, but pthread_join blocks when
 * the two threads are created/cancelled in various orders.
 */

#include <thread.h>
#include <unistd.h>

#include <assert.h>
#include <pthread.h>
#include <stdio.h>

int i, rc;
pthread_cond_t cond;
pthread_mutex_t mutex;

int condition = 0;
void * value;

void * thread(void * arg)
{
   int threadno = (int) arg;
   printf("t%d: alive\n", threadno);

   pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
   pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

   thr_yield();
   printf("t%d: async cancellation enabled\n", threadno);

   rc = pthread_mutex_lock(&mutex);
   printf("t%d: got mutex\n", threadno);

   /*
      if I make thread #1 the "tick" thread, things don't work
      this looks to be due to the order of thread creation/
      cancellation
   */

   if (threadno == 0)
   {
      for (;;)
      {
          printf("t%d: tick\n", threadno);
         sleep(1);
      }
   }
   else
   {
      while (!condition)
      {
         while (pthread_cond_wait(&cond, &mutex) != 0)
          {
             printf("this condition should never be met\n");
          }
      }
   }

   /* unreachable code */

   pthread_mutex_unlock(&mutex);
   printf("t%d: this should not happen; terminating\n", threadno);
   return 0;

int main()
{
   pthread_t ids[2];

   pthread_mutex_init(&mutex, NULL);
   pthread_cond_init(&cond, NULL);

   for (i = 0; i < 2; i++)
   {
      rc = pthread_create(&ids[i], NULL, thread, (void *) i);
      assert(rc == 0);
   }

   sleep(10);

   for (i = 0; i < 2; i++)
   {
      printf("tm: cancelling t%d\n", i, ids[i]);
      rc = pthread_cancel(ids[i]);
      assert(rc == 0);
      pthread_join(ids[i], &value);
      printf("tm: t%d has terminated\n", i);
   }

   pthread_cond_destroy(&cond);
   pthread_mutex_destroy(&mutex);

   return 0;



 Tue, 18 Jan 2000 03:00:00 GMT   
 Strange pthreads problem on Solaris

You didn't link it correctly. You need to link with
libpthread. The -mt switch links it with libthread, not with
libpthread. See the man page.



 Tue, 18 Jan 2000 03:00:00 GMT   
 
   [ 2 post ] 

Similar Threads

1. Strange SMP (pthreads) scalability problem

2. Problem porting Linux -> Solaris pthreads

3. Pthreads, signals, and Solaris problem

4. solaris pthreads performance problem

5. Pthreads problem with LDAP server for Solaris 2.6

6. Problem with stack size using pthreads in Solaris 2.5.1

7. Strange error with pthreads

8. strange pthreads behaviour


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software