Na documentação do Open Group , afirma-se que:
NAME
pthread_rwlock_wrlock, pthread_rwlock_trywrlock - lock a read-write lock object for writing
...
The pthread_rwlock_wrlock() and pthread_rwlock_trywrlock() functions may fail if:
...
[EDEADLK]
The current thread already owns the read-write lock for writing or reading.
Entretanto, quando executo o seguinte código:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
int main()
{
int error;
pthread_rwlock_t *mutex = (pthread_rwlock_t *)malloc(sizeof(pthread_rwlock_t));
if (mutex == NULL) {
perror("malloc");
abort();
}
pthread_rwlockattr_t attr;
error = pthread_rwlockattr_init(&attr);
if (error != 0)
{
fprintf(stderr, "%s:%d: pthread_rwlockattr_init failed: %s", __FILE__, __LINE__, strerror(error));
abort();
}
error = pthread_rwlock_init(mutex, &attr);
if (error != 0)
{
fprintf(stderr, "%s:%d: pthread_rwlock_init failed: %s", __FILE__, __LINE__, strerror(error));
abort();
}
error = pthread_rwlock_wrlock(mutex);
if (error != 0)
{
fprintf(stderr, "%s:%d: pthread_rwlock_wrlock failed: %s", __FILE__, __LINE__, strerror(error));
abort();
}
error = pthread_rwlock_rdlock(mutex);
if (error != 0)
{
fprintf(stderr, "%s:%d: pthread_rwlock_rdlock failed: %s", __FILE__, __LINE__, strerror(error));
abort();
}
return 0;
}
A saída é:
main.c:53: pthread_rwlock_rdlock failed: Resource deadlock avoided
Mas quando executo o seguinte código:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
int main()
{
int error;
pthread_rwlock_t *mutex = (pthread_rwlock_t *)malloc(sizeof(pthread_rwlock_t));
if (mutex == NULL) {
perror("malloc");
abort();
}
pthread_rwlockattr_t attr;
error = pthread_rwlockattr_init(&attr);
if (error != 0)
{
fprintf(stderr, "%s:%d: pthread_rwlockattr_init failed: %s", __FILE__, __LINE__, strerror(error));
abort();
}
error = pthread_rwlock_init(mutex, &attr);
if (error != 0)
{
fprintf(stderr, "%s:%d: pthread_rwlock_init failed: %s", __FILE__, __LINE__, strerror(error));
abort();
}
error = pthread_rwlock_rdlock(mutex);
if (error != 0)
{
fprintf(stderr, "%s:%d: pthread_rwlock_rdlock failed: %s", __FILE__, __LINE__, strerror(error));
abort();
}
error = pthread_rwlock_wrlock(mutex);
if (error != 0)
{
fprintf(stderr, "%s:%d: pthread_rwlock_wrlock failed: %s", __FILE__, __LINE__, strerror(error));
abort();
}
return 0;
}
Nesse caso, a pthread_rwlock_wrlock
chamada não retorna EDEADLK
e é bloqueada.
A explicação do Open Group está incorreta? Parece mais razoável que EDEADLK
seja devolvida. Qual poderia ser o problema aqui?
Testei isso no Linux usando o gcc, no meu computador com o WSL e também no OnlineGDB, e não funciona em nenhum desses ambientes. Parece ser um problema comum de implementação, e não um problema específico da minha configuração. Esse comportamento é intencional?