Si desbloquea un mutex ya desbloqueado, ¿está indefinido el comportamiento?
Si desbloquea un mutex ya desbloqueado, ¿es inseguro, seguro o indefinido el comportamiento?
El propósito de la pregunta está relacionado con el siguiente código, donde no sé si sería mejor desbloquear los mutex dentro del bloque if, o simplemente fuera del bloque if.
// This chunk of code makes dual locking semi-autonomous.
int c_lckd = 0, q_lckd = 0;
if (pthread_mutex_trylock(&crunch_mutex) == 0) c_lckd = 1;
if (pthread_mutex_trylock(&queue_mutex) == 0) q_lckd = 1;
if (q_lckd && !c_lckd) { QUEUE_UNLOCK; q_lckd = 0; }
else if (c_lckd && !q_lckd) { CRUNCH_UNLOCK; c_lckd = 0; }
if (c_lckd && q_lckd) {
printf("cr = %d, max = %d, cnt = %d\n",
crunching, max_crunching, queue_count(conn_queue));
if (crunching < max_crunching && queue_count(conn_queue)) {
pthread_t tid =
pthread_create(
&tid,
NULL,
crunch_conn,
(void *)queue_dequeue(conn_queue)
);
crunching++;
}
CRUNCH_UNLOCK QUEUE_UNLOCK
}
Gracias chenz