读者作家与监视器

时间:2017-11-11 01:28:52

标签: c pthreads monitor

我正在使用pthreads库尝试用监视器实现读写器应用程序。

我主要用以下方式调用我的线程函数

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

取决于每个线程访问关键部分的时间

1 个答案:

答案 0 :(得分:0)

代码将 相同的地址 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不受保护的。