I suspect I am doing something dumb here but I am getting seg faults on an embedded Linux platform (GCC compiler) when I try to run pthread_rwlock_init() on a rwlock embedded in a structure.
struct rwlock_flag {
int flag; // Flag
pthread_rwlock_t * rwlock; // Reader/writer lock for flag
};
The following causes a seg fault…
struct rwlock_flag * running;
running = (struct rwlock_flag *) malloc (sizeof(struct rwlock_flag));
rslt = pthread_rwlock_init(running->rwlock, NULL);
As does this…
pthread_rwlock_t * rwlock_dg2;
pthread_rwlock_init(rwlock_dg2,NULL);
However the following works fine…
pthread_rwlock_t rwlock_dg;
pthread_rwlock_init(& rwlock_dg,NULL);
Any thoughts?
Your first and second cases both try to initialise the rwlock for a pointer that points nowhere (well, technically, to a random or NULL location).
In the first case, you allocate space for your wrapper structure but not the
pthread_rwlock_t*within it. Hence it would point to a random location.This would work with an actual
pthread_rwlock_trather than a pointer to one:In the second, similarly, there’s no backing storage for
rwlock_dg2so it either points to a random location (if allocated within a function) or NULL (if declared at file level). You need:Your third case, which works, does so because the pointer
&rwlock_dgactually points to a realpthread_rwlock_t(which is of courserwlock_dg).