我正在使用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


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

最佳答案

该代码将相同的地址(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

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

10-11 00:02