2

根据 pthread_rwlock_rdlock 的 POSIX 文档,“如果写入者没有持有锁并且没有写入者被锁阻塞,则调用线程将获取读锁。” 我似乎发现即使写入器被阻止也可以获得读锁。我写的一个小样本的输出如下所示:

first reader acquiring lock...
first reader lock acquired
first writer acquiring lock...
second reader acquiring lock...
second reader lock acquired
first reader releasing lock
second reader releasing lock
first writer lock acquired
first writer releasing lock

关于我的代码有什么问题或我没有正确理解的任何建议?顺便一提:

$ make
gcc -g -I. -I../../isilib -c -Wpointer-arith -Wall -pedantic-errors
-D_POSIX_C_SOURCE=200809L -std=c99 -g rwlock_test1.c -o rwlock_test1.o
gcc rwlock_test1.o -L../../isilib -lisi -lrt -lm -pthread -o rwlock_test1
$ uname -a
Linux BLACKHEART 3.5.0-17-generic #28-Ubuntu SMP Tue Oct 9 19:31:23 UTC 
2012 x86_64 x86_64 x86_64 GNU/Linux
$ gcc --version
gcc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

#define PTH_create( a, b, c, d ) \
    (pthread_create( (a), (b), (c), (d) ) != 0 ? abort() : (void)0 )

#define PTH_join( a, b ) \
    (pthread_join( (a), (b) ) != 0 ? abort() : (void)0 )

#define PTH_rwlock_rdlock( a ) \
    (pthread_rwlock_rdlock( (a) ) != 0 ? abort() : (void)0 )

#define PTH_rwlock_wrlock( a ) \
    (pthread_rwlock_wrlock( (a) ) != 0 ? abort() : (void)0 )

#define PTH_rwlock_unlock( a ) \
    (pthread_rwlock_unlock( (a) ) != 0 ? abort() : (void)0 )

static void *firstReader(
    void *arg
);

static void *firstWriter(
    void *arg
);

static void *secondReader(
    void *arg
);

static pthread_rwlock_t rwlock  = PTHREAD_RWLOCK_INITIALIZER;

int main( int argc, char **argv )
{
    pthread_t   thr1;
    pthread_t   thr2;
    pthread_t   thr3;

    PTH_create( &thr1, NULL, firstReader, NULL );
    PTH_create( &thr2, NULL, firstWriter, NULL );
    PTH_create( &thr3, NULL, secondReader, NULL );

    PTH_join( thr1, NULL );
    PTH_join( thr2, NULL );
    PTH_join( thr3, NULL );

    return 0;
}

static void *firstReader( void *arg )
{
    printf( "first reader acquiring lock... \n" );
    PTH_rwlock_rdlock( &rwlock );
    printf( "first reader lock acquired \n" );
    sleep( 10 );
    printf( "first reader releasing lock \n" );
    PTH_rwlock_unlock( &rwlock );
    return NULL;
}

static void *firstWriter( void *arg )
{
    sleep( 2 );
    printf( "first writer acquiring lock... \n" );
    PTH_rwlock_wrlock( &rwlock );
    printf( "first writer lock acquired \n" );
    sleep( 10 );
    printf( "first writer releasing lock \n" );
    PTH_rwlock_unlock( &rwlock );
    return NULL;
}

static void *secondReader( void *arg )
{
    sleep( 5 );
    printf( "second reader acquiring lock... \n" );
    PTH_rwlock_rdlock( &rwlock );
    printf( "second reader lock acquired \n" );
    sleep( 5 );
    printf( "second reader releasing lock \n" );
    PTH_rwlock_unlock( &rwlock );
    return NULL;
}

附加信息:

从 posix 标准:宏_POSIX_THREAD_PRIORITY_SCHEDULING指示是否支持线程执行调度选项。来自unistd.h:“如果定义了这些符号,则相应的功能始终可用......”然后列出_POSIX_THREAD_PRIORITY_SCHEDULING。再次来自 posix:“如果支持 Thread Execution Scheduling 选项,并且锁中涉及的线程正在使用调度策略执行,SCHED_FIFO或者SCHED_RR,如果作者持有锁,则调用线程将不会获取锁......” 所以我有一个程序(如下),它显示在我的 Linux 系统上_POSIX_THREAD_PRIORITY_SCHEDULING,但我无法强制线程策略SCHED_RR(我也尝试过SCHED_FIFO,程序中没有显示)。

额外的想法?谢谢大家...

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

#define PTH_create( a, b, c, d ) \
    (pthread_create( (a), (b), (c), (d) ) != 0 ? abort() : (void)0 )

#define PTH_join( a, b ) \
    (pthread_join( (a), (b) ) != 0 ? abort() : (void)0 )

static void *driver(
    void *arg
);

int main( int argc, char **argv )
{
    pthread_attr_t  attr;
    pthread_attr_init( &attr );
    pthread_attr_setschedpolicy( &attr, SCHED_RR );

    pthread_t   thrID;

    PTH_create( &thrID, &attr, driver, NULL );
    printf( "%ld\n", _POSIX_THREAD_PRIORITY_SCHEDULING );
    struct sched_param  param;
    int                 policy;
    pthread_getschedparam( thrID, &policy, &param );
    if ( policy == SCHED_FIFO )
        puts( "SCHED_FIFO" );
    else if ( policy == SCHED_RR )
        puts( "SCHED_RR" );
    else if ( policy == SCHED_FIFO )
        puts( "SCHED_FIFO" );
    else if ( policy == SCHED_OTHER )
        puts( "SCHED_OTHER" );
    else
        puts( "eh?" );

    PTH_join( thrID, NULL );

    return 0;
}

static void *driver( void *arg )
{
    sleep( 2 );
    return NULL;
}

$ ./sched_test
200809
SCHED_OTHER
4

3 回答 3

3

您在 POSIX 中错过了这句话:

如果不支持 Thread Execution Scheduling 选项,则当写入者不持有锁且有写入者在锁上阻塞时,调用线程是否获取锁由实现定义。

您不能依赖 POSIX rwlocks 偏爱作者而不是读者。

于 2013-05-19T11:03:01.223 回答
2

看起来像是 Linux pthreads 实现中的一个错误。在 FreeBSD 上正常工作:

first reader acquiring lock... 
first reader lock acquired 
first writer acquiring lock... 
second reader acquiring lock... 
first reader releasing lock 
first writer lock acquired 
first writer releasing lock 
second reader lock acquired 
second reader releasing lock 
于 2013-05-18T00:12:29.497 回答
0

简单的说:

  • 任意数量的读者可以同时持有锁。
  • 一次最多有一个读者可以持有锁。
  • 读取器和写入器不能同时持有锁。

以你为例:

  • first reader acquiring lock...→ 好的,因为没有人持有锁
  • first reader lock acquired
  • first writer acquiring lock...→ 因为读卡器持有锁而阻塞
  • second reader acquiring lock...→ 好的,因为只有读者持有锁
  • second reader lock acquired
  • first reader releasing lock→ 第二个读者仍然持有锁
  • second reader releasing lock→ 没有人再拿着锁了
  • first writer lock acquired
  • first writer releasing lock

此外,没有通用的解决方案:第二个读取器可能会在第一个读取器之前释放锁,在这种情况下,上述行为将是有益的。或者第二个读取器可能运行很长时间,而第一个读取器和写入器很快完成,在这种情况下,严格按照请求的顺序授予资源是有益的。

于 2019-04-09T20:09:34.083 回答