我终于到目前为我正在创建的某个测试应用程序创建了一个准确的消费者 - 生产者类型模型,但最后一点是导致了一些问题。pthread,带有双指针的链表
我有2个结构为我的应用程序设置。一个用于链接列表,用作必须完成的工作列表。另一个是每个线程都有一个特定的结构,它包含一个指向链接列表的双指针。我不使用单个指针,因为我无法修改一个线程中的指针并检测到另一个线程中的变化。
//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;
在thread_specs
双指针获取绑定到list_of_work
结构的根部,像这样一个双指针:
主:
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;
这一切工作没有警告或错误。
现在我去创造我的并行线程有:
pthread_create(&thread1, NULL, worker, &thread1_info)
,并在我的并行线程正进行2个铸件,1铸就的thread_info结构和其他投链表。 ptr是我的参数:
thread_specs *thread = (thread_specs *)ptr;
list_of_work *work_list = (list_of_work *)thread->linked_list;
list_of_work *temp;
这不会引发错误。
然后,我有一个函数称为list_of_work *get_work(list_of_work *ptr)
,该函数起作用,所以我不会发布整个事情,但正如你可以看到它期望看到一个指向链接列表的指针,并且它返回一个相同链接的指针清单(这是NULL
或者是下一部分工作)。
所以我用这个函数来获取下一项工作是这样的:
temp = get_work(*work_list);
if (temp != NULL) {
work_list = &temp;
printf("thread: %d || found work, printing type of work.... ",thread->thread_id);
}
现在,这是问题的关键。我该如何正确地投射并将指针传递给我的get_work()
函数的第一个指针,以便它能够完成它的工作。
我的编译器吐出警告:
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’
我感谢谁可以HEP我!
'thread-> drives':没有这样的成员。也许发布* real *代码会很有帮助。 – WhozCraig
啊这是真实的代码。我只是改变了一些引用,使我的意图更清晰 – RG337
演员也是不正确的(它的看似必要性应该是一个红旗,它不正确)。 'list_of_work * work_list =(list_of_work *)thread-> linked_list;'应该是'list_of_work * work_list = *(thread-> linked_list);',尽管老实说,如果你试图共享和互斥控制一个链表头指针这不是做这件事的方法。 – WhozCraig