I have finally got so far to create an accurate consumer-producer type model for some test application I am making but the last bit is causing me some problems.
I have 2 structs set-up for my application. One is used for a linked list which is used as a list of work that has to be done. The other one is a struct specific to each thread which contains a double pointer to the linked list. I don`t use a single pointer as then I am unable to modify the pointer in one thread and detect a change in the other.
//linked list struct:
typedef struct list_of_work list_of_work;
struct list_of_work {
// information for the work
list_of_work *next;
};
//thread struct:
typedef struct thread_specs {
list_of_work **linked_list;
unsigned short thread_id;
pthread_mutex_t *linked_list_mtx;
} thread_specs;
the double pointer in thread_specs
get bound to a double pointer of the root of the list_of_work
struct like so:
in main:
list_of_work *root;
list_of_work *traveller;
pthread_t thread1;
thread_specs thread1_info;
// allocating root and some other stuff
traveller = root;
thread1_info.linked_list = &traveller;
This all works without warnings or errors.
now I go on to create my pthread with:
pthread_create(&thread1, NULL, worker, &thread1_info )
and in my pthread I perform 2 casts, 1 to cast the thread_info struct and the other to cast the linked list. ptr is my argument:
thread_specs *thread = (thread_specs *)ptr;
list_of_work *work_list = (list_of_work *)thread->linked_list;
list_of_work *temp;
this throws no errors.
I then have a function which is called list_of_work *get_work(list_of_work *ptr)
, the function works so I won't post the entire thing but as you can see it expects to see a pointer to the linked list and it returns a pointer of the same linked list(which is either NULL
or is the next piece of work).
So I use this function to get the next piece of work like this:
temp = get_work(*work_list);
if (temp != NULL) {
work_list = &temp;
printf("thread: %d || found work, printing type of work.... ",thread->thread_id);
}
Now this is the crux. How can I correctly cast and pass the pointer BEHIND the first pointer to my get_work()
function so it can do what it does.
my compiler spits warnings:
recode.c:348:9: error: incompatible type for argument 1 of ‘get_work’
recode.c:169:14: note: expected ‘struct list_of_work *’ but argument is of type ‘list_of_work’
I thank whoever can hep me!
Based on the function get_work()
's definition you posted and error message, this issue here:
temp = get_work(work_list);
^
/* Notice there's no dereferencing here */
The function expects a pointer to struct list_of_work
whereas you pass a struct list_of_work
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.