Pthread_rwlock_wlock nach pthread_rwlock_rdlock gibt edeadlk nicht zurückLinux

Linux verstehen
Anonymous
 Pthread_rwlock_wlock nach pthread_rwlock_rdlock gibt edeadlk nicht zurück

Post by Anonymous »

In der Dokumentation bei Open Group heißt es: < /p>

Code: Select all

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>
Wenn ich jedoch den folgenden Code ausführe: < /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>
Die Ausgabe ist: < /p>
main.c:53: pthread_rwlock_rdlock failed: Resource deadlock avoided
< /code>
Aber wenn ich den folgenden Code ausführe: < /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;
}
In diesem Fall gibt der Aufruf von pThread_rwlock_wlock edeadlk nicht zurück und blockiert stattdessen. Es scheint vernünftiger, dass Edeadlk zurückgegeben wird. Was könnte hier das Problem sein? Dies scheint eher ein häufiges Implementierungsproblem als ein für mein Setup spezifischer Problem zu sein. Ist dieses Verhalten beabsichtigt?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post