Reputation: 43
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
Reputation: 2867
This is a relativly simple option to test your implementation, this is not the only existing option.
Create a Function which retrieves the same data in a Loop.
Create few Threads (pthread_create) and assign to each one of the functions.
Upvotes: 1