by Anonymous » 07 Apr 2025, 22:36
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?
In der Dokumentation bei Open Group heißt es: < /p>
[code]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;
}
[/code]
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 [url=viewtopic.php?t=20324]Problem[/url] sein? Dies scheint eher ein häufiges Implementierungsproblem als ein für mein Setup spezifischer [url=viewtopic.php?t=20324]Problem[/url] zu sein. Ist dieses Verhalten beabsichtigt?