/** * Starts 4 pthreads and uses either a pthread_rwlock_t or a di_rwlock_t * to protect access to an array. * */ int main(void) { int i; pthread_t *pthread_list; int numThreads = 0; int threadList[] = { 1, 2, 4, 8, 16, 24, 32, 48, 64, 128 }; // size is 10 /* Allocate memory for the two instance arrays */ array1 = (int *)malloc(ARRAY_SIZE*sizeof(int)); if (array1 == NULL) { printf("Not enough memory to allocate array\n"); return -1; } for (i = 0; i < ARRAY_SIZE; i++) array1[i] = 0; /* Initialize locks */ pthread_mutex_init(&pmutex, NULL); ticket_mutex_init(&ticketmutex); tidex_mutex_init(&tidexmutex); printf("Starting benchmark with %d threads\n", NUM_THREADS); printf("Array has size of %d\n", ARRAY_SIZE); // Create the threads pthread_list = (pthread_t *)calloc(sizeof(pthread_t), NUM_THREADS); for (int i = 0; i < 10; i ++) { printf("\n---------- Active threads: %d ----------\n", threadList[i]); singleTest(threadList[i], "pthread_mutex_t, sleeping for 10 seconds...\n", TYPE_PTHREAD_MUTEX, pthread_list); singleTest(threadList[i], "ticket_mutex_t, sleeping for 10 seconds...\n", TYPE_TICKET_MUTEX, pthread_list); singleTest(threadList[i], "tidex_mutex_t, sleeping for 10 seconds...\n", TYPE_TIDEX_MUTEX, pthread_list); } /* Destroy locks */ pthread_mutex_destroy(&pmutex); ticket_mutex_destroy(&ticketmutex); tidex_mutex_destroy(&tidexmutex); /* Release memory for the array instances and threads */ free(array1); free(pthread_list); return 0; }
/** * Starts 4 pthreads and uses either a pthread_rwlock_t or a di_rwlock_t * to protect access to an array. * */ int main(void) { int i; pthread_t *pthread_list; int threadid[NUM_THREADS]; /* Allocate memory for the two instance arrays */ array1 = (int *)malloc(ARRAY_SIZE*sizeof(int)); if (array1 == NULL) { printf("Not enough memory to allocate array\n"); return -1; } for (i = 0; i < ARRAY_SIZE; i++) { array1[i] = 0; } /* Initialize locks */ pthread_mutex_init(&pmutex, NULL); pthread_spin_init(&pspin, PTHREAD_PROCESS_PRIVATE); mpsc_mutex_init(&mpscmutex); ticket_mutex_init(&ticketmutex); clh_mutex_init(&clhmutex); printf("Starting benchmark with %d threads\n", NUM_THREADS); printf("Array has size of %d\n", ARRAY_SIZE); // Create the threads pthread_list = (pthread_t *)calloc(sizeof(pthread_t), NUM_THREADS); printf("Doing test for pthread_mutex_t, sleeping for 10 seconds...\n"); g_which_lock = TYPE_PTHREAD_MUTEX; clearOperCounters(); // Start the threads for(i = 0; i < NUM_THREADS; i++ ) { threadid[i] = i; pthread_create(&pthread_list[i], NULL, (void *(*)(void *))worker_thread, (void *)&threadid[i]); } sleep(10); g_quit = 1; for (i = 0; i < NUM_THREADS; i++) { pthread_join(pthread_list[i], NULL); } g_quit = 0; printOperationsPerSecond(); printf("Doing test for pthread_spin_t, sleeping for 10 seconds\n"); g_which_lock = TYPE_PTHREAD_SPIN; clearOperCounters(); for(i = 0; i < NUM_THREADS; i++ ) { pthread_create(&pthread_list[i], NULL, (void *(*)(void *))worker_thread, (void *)&threadid[i]); } sleep(10); g_quit = 1; for (i = 0; i < NUM_THREADS; i++) { pthread_join(pthread_list[i], NULL); } g_quit = 0; printOperationsPerSecond(); printf("Doing test for mpsc_mutex_t, sleeping for 10 seconds\n"); g_which_lock = TYPE_MPSC_MUTEX; clearOperCounters(); for(i = 0; i < NUM_THREADS; i++ ) { pthread_create(&pthread_list[i], NULL, (void *(*)(void *))worker_thread, (void *)&threadid[i]); } sleep(10); g_quit = 1; for (i = 0; i < NUM_THREADS; i++) { pthread_join(pthread_list[i], NULL); } g_quit = 0; printOperationsPerSecond(); printf("Doing test for ticket_mutex_t, sleeping for 10 seconds...\n"); g_which_lock = TYPE_TICKET_MUTEX; clearOperCounters(); // Start the threads for(i = 0; i < NUM_THREADS; i++ ) { threadid[i] = i; pthread_create(&pthread_list[i], NULL, (void *(*)(void *))worker_thread, (void *)&threadid[i]); } sleep(10); g_quit = 1; for (i = 0; i < NUM_THREADS; i++) { pthread_join(pthread_list[i], NULL); } g_quit = 0; printOperationsPerSecond(); printf("Doing test for exchg_mutex_t, sleeping for 10 seconds...\n"); g_which_lock = TYPE_EXCHG_MUTEX; clearOperCounters(); // Start the threads for(i = 0; i < NUM_THREADS; i++ ) { threadid[i] = i; pthread_create(&pthread_list[i], NULL, (void *(*)(void *))worker_thread, (void *)&threadid[i]); } sleep(10); g_quit = 1; for (i = 0; i < NUM_THREADS; i++) { pthread_join(pthread_list[i], NULL); } g_quit = 0; printOperationsPerSecond(); /* Destroy locks */ pthread_mutex_destroy(&pmutex); pthread_spin_destroy(&pspin); mpsc_mutex_destroy(&mpscmutex); ticket_mutex_destroy(&ticketmutex); clh_mutex_destroy(&clhmutex); /* Release memory for the array instances and threads */ free(array1); free(pthread_list); return 0; }