我正在尝试在无限的while循环中随机创建读取器和写入器线程(每个5个)。编写器应等到所有读取器都执行完毕。为此,我正在使用互斥体和条件变量。
// Global variables
int readerCount=0, writerCount=0, sharedVariable=1001;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER;
读取器线程运行以下函数
void *readShared(void* param)
{
int shVariable, readers; //To copy and print global variables locally
pthread_mutex_lock(&m);
readerCount++;
while(readerCount > 1)
{
pthread_cond_wait(&read_cond, &m);
}
readerCount--;
readers = readerCount;
shVariable = sharedVariable;
pthread_mutex_unlock(&m);
printf("[Reader]: Value in shared Variable = %dn", shVariable);
printf("Other "Reader" threads present = %dn", readers);
pthread_mutex_lock(&m);
if(readerCount > 0)
{
pthread_cond_signal(&read_cond);
}
else if(writerCount > 0)
{
pthread_cond_signal(&write_cond);
}
pthread_mutex_unlock(&m);
}
编写器线程运行以下函数:
void *writeShared(void *valueToWrite)
{
//To copy and print global variables
int value = *(int *)(valueToWrite), readers, shVariable;
pthread_mutex_lock(&m);
writerCount++;
while(readerCount > 0 || writerCount > 1)
{
pthread_cond_wait(&write_cond, &m);
}
sharedVariable = value;
readers = readerCount;
shVariable = sharedVariable;
writerCount--;
pthread_mutex_unlock(&m);
printf("[Writer]: Value in shared Variable = %dn", shVariable);
printf("Value to write = %dn", value);
printf("Other "Reader" threads present = %dn", readers);
pthread_mutex_lock(&m);
if(readerCount > 0)
{
pthread_cond_signal(&read_cond);
}
else if(writerCount > 0)
{
pthread_cond_signal(&write_cond);
}
pthread_mutex_unlock(&m);
}
主要功能:
int main()
{
pthread_t readers[5], writers[5];
int readerIndex=0, writerIndex=0;
while(1)
{
if(5 == readerIndex && 5 == writerIndex)
{
break;
}
if(rand() % 2)
{
if(readerIndex < 5)
{
pthread_create(&readers[readerIndex++], NULL, readShared, NULL);
}
}
else
{
if(writerIndex < 5)
{
pthread_create(&writers[writerIndex++], NULL, writeShared, (void*)((rand() % 1000) + 100));
}
}
}
for(int i=0; i<5; i++)
{
pthread_join(readers[i], NULL);
pthread_join(writers[i], NULL);
}
return 0;
}
我尝试使用 gdb 运行并获得了以下内容:
Starting program: /home/abhijeet/threads/readWrite
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff77c4700 (LWP 3232)]
[Reader]: Value in shared Variable = 1001
Other "Reader" threads present = 0
[New Thread 0x7ffff6fc3700 (LWP 3233)]
[Thread 0x7ffff77c4700 (LWP 3232) exited]
[New Thread 0x7ffff67c2700 (LWP 3234)]
[Reader]: Value in shared Variable = 1001
Other "Reader" threads present = 0
[New Thread 0x7ffff5fc1700 (LWP 3235)]
[Thread 0x7ffff67c2700 (LWP 3234) exited]
Thread 3 "readWrite" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff6fc3700 (LWP 3233)]
0x000055555555494a in writeShared (valueToWrite=0x36d) at PriorityReadWrite.c:13
13 int value = *(int *)(valueToWrite), readers, shVariable;
这一行:
pthread_create(&writers[writerIndex++], NULL, writeShared, (void*)((rand() % 1000) + 100));
将100 到 1099 之间的随机整数作为参数传递给writeShared
函数。在使用 GDB 调试的特定调用中,随机值为0x36d
或877
。
这一行:
int value = *(int *)(valueToWrite)
获取传递的整数并将其解释为整数的地址(它不是),从而导致分段错误。
要解决此问题,您需要传入实际整数的地址(在main
中创建int valueToWrite[5]
数组,并将&valueToWrite[writerIndex]
传递给pthread_create
(请注意在创建线程后递增writerIndex
)。
或者,您可以将writeShared
修复为需要整数而不是地址:
int value = (int)valueToWrite;