简体   繁体   English

pthread,带有双指针的链接列表

[英]pthread, linked list with a double pointer

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. 我为我的应用程序设置了2个结构。 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: thread_specs的双指针绑定到list_of_work结构根的双指针,如下所示:

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:

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. 在我的pthread中,我执行2次强制转换,执行1次强制转换thread_info结构,另一个强制转换链表。 ptr is my argument: ptr是我的观点:

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). 然后,我有一个名为list_of_work *get_work(list_of_work *ptr)的函数,该函数可以正常工作,因此我不会发布整个内容,但是如您所见,它希望看到指向链表的指针,并且返回相同的链表(它为NULL或为下一工作)。

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. 如何正确地将指针转换为第一个指针,并将其传递给get_work()函数,使其可以执行其操作。

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: 根据您发布的函数get_work()的定义和错误消息,此问题在这里:

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 . 该函数需要一个指向 struct list_of_work指针 ,而您传递一个struct list_of_work

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM