簡體   English   中英

pthread,帶有雙指針的鏈接列表

[英]pthread, linked list with a double pointer

到目前為止,我終於可以為我正在做的一些測試應用程序創建一個准確的消費者-生產者類型的模型,但是最后一點給我帶來了一些問題。

我為我的應用程序設置了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:

pthread_create(&thread1, NULL, worker, &thread1_info )

在我的pthread中,我執行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’

我感謝誰可以幫助我!

根據您發布的函數get_work()的定義和錯誤消息,此問題在這里:

temp = get_work(work_list);
                ^
   /* Notice there's no dereferencing here */

該函數需要一個指向 struct list_of_work指針 ,而您傳遞一個struct list_of_work

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM