Using pthread on Linux 2.4 - getting “incomplete type” compilation error
-
14-11-2019 - |
문제
I'm working on Linux 2.4 (doing h.w for my O.S course), I want to use pthread to implement a reader-writer lock. In rw_lock.c I have:
#include <pthread.h>
#include <stdlib.h>
#include "rw_lock.h"
struct readers_writers_t
{
int prio;
int number_of_readers;
pthread_cond_t no_readers;
int number_of_writers;
int number_of_waiting_writers;
pthread_cond_t no_writers;
pthread_mutex_t lock;
};
[functions...]
in rw_lock.h I have:
typedef struct readers_writers_t readers_writers;
In another C file (implementation of linked list) I have :
#include "rw_lock.h"
struct LinkedList
{
ListNode* head;
ListNode* tail;
readers_writers rwLock;
};
(and more functions,includes etc').
I get (one) compilation error :
"rwLock has incomplete type".
I have no idea why I get this error (or how to fix it...).
help is appreciated,thanks!
해결책
Your linked list C file does not know about the struct readers_writers_t
, it's defined within your rw_lock.c file.
You should just move your struct readers_writers_t out of rw_lock.c and into your rw_lock.h file.
Or perhaps you don't want your linked list to know about that struct. In that case, you should define the
readers_writers rwLock
as a pointer (readers_writers *rwLock) rather. Another alternative is to make your readers_writers typedef a pointer:typedef struct readers_writers_t *readers_writers
This requires that functions in rw_lock.c allocates space for the actual struct, as that's the only file that knows about the actual struct.
다른 팁
You need to move the structure defintion from the source file to the header file.
rw_lock.h should be:
#ifndef SOME_UNIQUE_STRING_MY_RW_LOCK_H
#define SOME_UNIQUE_STRING_MY_RW_LOCK_H
#include <pthread.h>
struct readers_writers_t
{
int prio;
int number_of_readers;
pthread_cond_t no_readers;
int number_of_writers;
int number_of_waiting_writers;
pthread_cond_t no_writers;
pthread_mutex_t lock;
};
typedef struct readers_writers_t readers_writers;
#endif