gpt4 book ai didi

c - 带显示器的读写器

转载 作者:行者123 更新时间:2023-11-30 16:39:01 26 4
gpt4 key购买 nike

我正在使用 pthreads 库来尝试实现带有监视器的读取器/写入器应用程序。

我通过以下方式在 main 中调用我的线程函数

pthread_t *tid;
tid = (pthread_t*)malloc(sizeof(pthread_t)*(r + w));
int addr = 0;
//Create r # readers
for (int a = 0; a < r; a++)
{
pthread_create(&tid[addr], NULL, Reader, (void*)&a);
addr++;
}

然后他们通过以下3个函数调用完成他们的功能

void *Reader(void *pid)
{
static int *id = (int*)pid;
for (int i = 0; i < 10; i++)
{
beginRead(id);
endRead();
#ifdef __unix
usleep(R);
#endif
#ifdef _WIN32
Sleep(R);
#endif
}
return 0;
}

void beginRead(int *id)
{
//If writelock or writer_queued then wait(canRead)
pthread_mutex_lock(&writelock);
while (writer_queued)
{
reader_queued = true;
pthread_cond_wait(&canRead, &writelock);
reader_queued = false;
}
readers++;
pthread_mutex_unlock(&writelock);
pthread_mutex_lock(&output);
time_t t = time(0);
struct tm * now = localtime(&t);
fprintf(outputfile, "DB value read =:%d:%d by reader number: %d\n", now- >tm_sec, (now->tm_sec) / 1000, (int)*id);
printf("DB value read =:%d:%d by reader number: %d\n", now->tm_sec, (now->tm_sec)/1000, (int)*id);
pthread_mutex_unlock(&output);
pthread_cond_broadcast(&canRead);
}

void endRead()
{
--readers; // just finished a reader
if (readers == 0)
{
pthread_cond_signal(&canWrite);
}
}

这应该使每个线程进入代码的关键部分 10 次(因此,如果创建了 2 个线程,它将访问关键部分 20 次)我让线程访问代码的关键部分正确的次数,我只是无法让它打印正确的线程 ID#

所以如果我用 r=2 和 R=200 调用这个函数,我的输出将是:

DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2

当它应该是类似的东西

DB value read =:30:0 by reader number:1
DB value read =:30:0 by reader number:1
DB value read =:30:0 by reader number:1
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number:1
DB value read =:30:0 by reader number:1
DB value read =:30:0 by reader number:1
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number:1
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number:1
DB value read =:30:0 by reader number:1
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number:1
DB value read =:30:0 by reader number:1
DB value read =:30:0 by reader number:1
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2
DB value read =:30:0 by reader number: 2

取决于每个线程访问临界区的时间

最佳答案

代码将相同的地址(a的地址)传递给每个线程。

您想向每个线程传递一个单独的地址。

您可以像这样执行此操作:

struct thread_info
{
pthread_t tid;
int id;
}

...

struct thread_info * pti = malloc((r + w) * sizeof *pit);
size_t addr = 0;

//Create r # readers
for (int a = 0; a < r; a++)
{
pti[addr]->id = a;
pthread_create(&pti[addr]->tid, NULL, Reader, &pti[addr]->id);

addr++;
}
<小时/>

还有这一行

  --readers; // just finished a reader

并发访问readers不 protected 。

关于c - 带显示器的读写器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47233418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com