Код: Выделить всё
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.
< /code>
Однако, когда я запускаю следующий код: < /p>
#include
#include
#include
#include
#include
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;
}
< /code>
Выход: < /p>
main.c:53: pthread_rwlock_rdlock failed: Resource deadlock avoided
< /code>
Но когда я запускаю следующий код: < /p>
#include
#include
#include
#include
#include
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;
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... rn-edeadlk