KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I wanted to know what would be better/faster to use POSIX calls like pthread_once() and sem_wait() or the dispatch_* functions, so I created a little test and am surprised at the results (questions and results are at the end). In the test code I am using mach_absolute_time() to time the calls. I really don’t care that this is not exactly matching up with nano-seconds; I am comparing the values with each other so the exact time units don't matter, only the differences between the interval do. The numbers in the results section are repeatable and not averaged; I could have averaged the times but I am not looking for exact numbers. test.m (simple console application; easy to compile): #import <Foundation/Foundation.h> #import <dispatch/dispatch.h> #include <semaphore.h> #include <pthread.h> #include <time.h> #include <mach/mach_time.h> // *sigh* OSX does not have pthread_barrier (you can ignore the pthread_barrier // code, the interesting stuff is lower) typedef int pthread_barrierattr_t; typedef struct { pthread_mutex_t mutex; pthread_cond_t cond; int count; int tripCount; } pthread_barrier_t; int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count) { if(count == 0) { errno = EINVAL; return -1; } if(pthread_mutex_init(&barrier->mutex, 0) < 0) { return -1; } if(pthread_cond_init(&barrier->cond, 0) < 0) { pthread_mutex_destroy(&barrier->mutex); return -1; } barrier->tripCount = count; barrier->count = 0; return 0; } int pthread_barrier_destroy(pthread_barrier_t *barrier) { pthread_cond_destroy(&barrier->cond); pthread_mutex_destroy(&barrier->mutex); return 0; } int pthread_barrier_wait(pthread_barrier_t *barrier) { pthread_mutex_lock(&barrier->
Tags (comma-separated)
Save Edits
Cancel