Quoting your code:
free(q);
if (err = pthread_rwlock_unlock(q->data->rwlock)) {
q is freed, and then use as an argument to pthread_rwlock_unlock().
Wow. Sorry, usually I'm not SO stupid. But anyway this was not the problem
I was talking about. Moving "free(q)" down didn't affect anything.
Whats the problem with SCHED_FIFO I wonder?