c - 结构问题的定义

标签 c linux

我已经声明了如下所示的结构,当我编译时,我有很多错误,例如:

error: 'struct RW' has no member named 'num_writes'
error: 'struct RW' has no member named 'writer_cv'
error: 'struct RW' has no member named 'reader_cv'
error: 'struct RW' has no member named 'lock'

我认为使用前向声明可以解决问题,但似乎并非如此。或者我做错了?

#include <stdio.h>
#include <pthread.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

struct RW;
struct RW{
    volatile int num_reads_in_progress;
    volatile int num_writes;
    pthread_cond_t reader_cv;
    pthread_cond_t writer_cv;
    pthread_mutex_t lock;
};
char *buf;


   //Precondition: b->lock must be locked before this function is called
   void signal_next(struct RW *b){
        if(b->num_writes > 0){
            //if any writes are waiting wake one up
            pthread_cond_signal(&b->writer_cv);
        }
        else{
            //if are no writes pending, wake up all the readers
            pthread_cond_broadcast(&b->reader_cv);
        }
    }



  char *ts_read(struct RW *b){

    pthread_mutex_lock(&b->lock);
    while(b->num_writes > 0){
        //cond_wait unlocks the mutex, waits to be signaled, then re-acquires the mutex
        pthread_cond_wait(&b->reader_cv,&b->lock);
    }
    //By there b->num_writes must be 0
    b->num_reads_in_progress++;
    pthread_mutex_unlock(&b->lock);

    buf = read(b);

    pthread_mutex_lock(&b->lock);
    b->num_reads_in_progress--;
    signal_next(b);
    pthread_mutex_unlock(&b->lock);
}

    void ts_write(struct RW *b) {
    pthread_mutex_lock(&b->lock);
    b->num_writes++;

    if (b->num_writes > 1 || b->num_reads_in_progress > 0)
    {
        // cond_wait unlocks the mutex, waits to be signaled,
        // then re-acquires the mutex
        pthread_cond_wait(&b->writer_cv, &b->lock);
    }
    pthread_mutex_unlock(&b->lock);

    write(b, buf);
    pthread_mutex_lock(&b->lock);
    b->num_writes--;
    signal_next(b);
    pthread_mutex_unlock(&b->lock);
}
   int main(){
   pthread_t white[3];
   pthread_t black[3];
   struct RW *rw ;
   rw = malloc(sizeof RW);
   int i;
   for(i = 0; i < 3; i++){
      pthread_create(&white[i],NULL,&ts_read,&rw);
   }

   for(i = 0; i < 3; i++){
      pthread_create(&black[i],NULL,ts_write,&rw);
   }
   for(i = 0; i < 3; i++){
      pthread_join(white[i],NULL);
   }
   for(i = 0; i < 3; i++){
      pthread_join(black[i],NULL);
   }
   return 0;


}

错误:

error: expected ':', ',', ';','}'  or '_attribute_'before 'num_writes'
In function 'signal_next':
error: 'struct RW' has no member named 'num_writes'
error: 'struct RW' has no member named 'writer_cv'
error: 'struct RW' has no member named 'reader_cv'
In function 'ts_read':
error: 'struct RW' has no member named 'num_writes'
error: 'struct RW' has no member named 'writer_cv'
error: 'struct RW' has no member named 'reader_cv'
error: 'struct RW' has no member named 'lock'

ts_readts_writemain 的错误相同

最佳答案

此代码在使用 GCC 4.8.2 的 Mac OS X 10.9 Mavericks 上使用命令行进行编译时不会出现警告:

gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -c ptc.c

由于使用的编译器选项,函数定义之前的函数声明是必要的;我通常通过使函数静态而不是重复自己来解决这个问题,但这取决于其他文件中是否需要这些函数。如果其他地方需要它们,那么当然将声明放在 header 中。

我已使用 /* Fix */ 标记了问题中代码的大部分更改。

#include <stdio.h>
#include <pthread.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

struct RW;
struct RW
{
    volatile int num_reads_in_progress;
    volatile int num_writes;
    pthread_cond_t reader_cv;
    pthread_cond_t writer_cv;
    pthread_mutex_t lock;
};
char *buf;
void signal_next(struct RW *b);

// Precondition: b->lock must be locked before this function is called
void signal_next(struct RW *b)
{
    if (b->num_writes > 0)
    {
        // if any writes are waiting wake one up
        pthread_cond_signal(&b->writer_cv);
    }
    else
    {
        // if are no writes pending, wake up all the readers
        pthread_cond_broadcast(&b->reader_cv);
    }
}

extern char *xx_read(struct RW *); /* Fix */

void *ts_read(void *vb);
void *ts_read(void *vb) /* Fix */
{
    struct RW *b = vb; /* Fix */
    pthread_mutex_lock(&b->lock);
    while (b->num_writes > 0)
    {
        // cond_wait unlocks the mutex, waits to be signaled, then re-acquires the mutex
        pthread_cond_wait(&b->reader_cv, &b->lock);
    }
    // By there b->num_writes must be 0
    b->num_reads_in_progress++;
    pthread_mutex_unlock(&b->lock);

    buf = xx_read(b); /* Fix */

    pthread_mutex_lock(&b->lock);
    b->num_reads_in_progress--;
    signal_next(b);
    pthread_mutex_unlock(&b->lock);
    return 0; /* Fix */
}

extern void xx_write(struct RW *, char *); /* Fix */
void *ts_write(void *vb);
void *ts_write(void *vb) /* Fix */
{
    struct RW *b = vb; /* Fix */
    pthread_mutex_lock(&b->lock);
    b->num_writes++;

    if (b->num_writes > 1 || b->num_reads_in_progress > 0)
    {
        // cond_wait unlocks the mutex, waits to be signaled,
        // then re-acquires the mutex
        pthread_cond_wait(&b->writer_cv, &b->lock);
    }
    pthread_mutex_unlock(&b->lock);

    xx_write(b, buf); /* Fix */
    pthread_mutex_lock(&b->lock);
    b->num_writes--;
    signal_next(b);
    pthread_mutex_unlock(&b->lock);
    return 0; /* Fix */
}

int main(void)
{
    pthread_t white[3];
    pthread_t black[3];
    struct RW *rw;
    rw = malloc(sizeof(struct RW)); /* Fix */
    int i;
    for (i = 0; i < 3; i++)
    {
        pthread_create(&white[i], NULL, &ts_read, &rw);
    }

    for (i = 0; i < 3; i++)
    {
        pthread_create(&black[i], NULL, ts_write, &rw);
    }
    for (i = 0; i < 3; i++)
    {
        pthread_join(white[i], NULL);
    }
    for (i = 0; i < 3; i++)
    {
        pthread_join(black[i], NULL);
    }
    return 0;
}

我创建了这个社区 Wiki,因为在我之前其他人付出了很多努力。我没有阅读所有评论,但它们似乎涵盖了与我所做的更改大致相同的基础。

关于c - 结构问题的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20280714/

相关文章:

c++ - 避开 CRT

c - const struct pointer 是否保证内存损坏/崩溃问题的安全性?

linux - DNSmasq + DHCP : how to return empty DNS to all clients

linux - 中断处理程序可以被抢占吗?

linux - jpegtran 优化而不更改文件名

c - 为什么 __last__ 元素需要 c struct 填充

c - 描述以下 C 代码实现的内容

c - 为什么无论输入是什么都会执行两个 if 语句?

linux - 如何在 linux 上找到所有基本上是其他目录或文件的软链接(soft link)或硬链接(hard link)的文件?

python - 将 "python2.7 -c"输出作为 C 文件的参数传递