michaela
michaela

Reputation: 43

How can I test a thread-safe implementation in c?

I have a thread-safe program which I would like to test (see below). I am not sure how to proceed with starting a test program as this would be my first test program. The result should be a demonstration that uses this thread-safe program implementation and why it is the best alternativ to test a thread-safe program like the example.

#include <errno.h>
#include <pthread.h>
static pthread_mutex_t listlock = PTHREAD_MUTEX_INITIALIZER;

int accessdata_r(void) {  /* return nonnegative traversal key if successful */ 
   int error;   
   int key;
   if (error = pthread_mutex_lock(&listlock)) {        /* no mutex, give up */
      errno = error;
      return -1; 
   }
   key = accessdata();
   if (key == -1) {
      error = errno;
      pthread_mutex_unlock(&listlock);
      errno = error;
      return -1;
   }
   if (error = pthread_mutex_unlock(&listlock)) {
      errno = error;
      return -1;
   }
   return key;
}

int adddata_r(data_t data) {        /* allocate a node on list to hold data */
   int error;
   if (error = pthread_mutex_lock(&listlock)) {        /* no mutex, give up */
      errno = error;
      return -1;
   }
   if (adddata(data) == -1) {
      error = errno;
      pthread_mutex_unlock(&listlock);
      errno = error;
      return -1;
   }
   if (error = pthread_mutex_unlock(&listlock)) {
      errno = error;
      return -1;
   }
   return 0; 
}

int getdata_r(int key, data_t *datap) {             /* retrieve node by key */
   int error;
   if (error = pthread_mutex_lock(&listlock)) {        /* no mutex, give up */
      errno = error;
      return -1;
   }
   if (getdata(key, datap) == -1) {
      error = errno;
      pthread_mutex_unlock(&listlock);
      errno = error;
      return -1;
   }
   if (error = pthread_mutex_unlock(&listlock)) {
      errno = error;
      return -1;
   }
   return 0; 
}

int freekey_r(int key) {                                    /* free the key */
   int error;
   if (error = pthread_mutex_lock(&listlock)) {        /* no mutex, give up */
      errno = error;
      return -1;
   }
   if (freekey(key) == -1) {
      error = errno;
      pthread_mutex_unlock(&listlock);
      errno = error;
      return -1;
   }
   if (error = pthread_mutex_unlock(&listlock)) {
      errno = error; 
      return -1;   
   } 
   return 0;  
}

Upvotes: 4

Views: 1387

Answers (1)

Michael
Michael

Reputation: 2867

This is a relativly simple option to test your implementation, this is not the only existing option.

  1. Create a Function which will add X items to the list (It could be regular counter)
  2. Create a Function which retrieves the same data in a Loop.

  3. Create few Threads (pthread_create) and assign to each one of the functions.

  4. This way you see if there are no dedlocks, in-order to check correctness, you should add some type of timestamp during your insert (you need to insert a value tuppled with timestamp), hold some array for each thread which will hold the results , at the end print your results and see if the timestamps preserve correctness.

Upvotes: 1

Related Questions