根据 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, ¶m );
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