對于學校,我正在開發一個專案,該專案有 2 個正在運行的讀取執行緒和 1 個圍繞共享緩沖區作業的寫入執行緒。這個共享緩沖區是我們自己編程的某種基于指標的串列。為了使其執行緒安全,我曾經使用 pthread_rw_locks 和一些 pthread_barriers。當我嘗試運行我的代碼時,它幾乎立即崩潰了,并且給了我一個分段錯誤。使用 gdb 除錯器時,它給了我以下訊息:
程式收到信號SIGSEGV,分段錯誤。
__pthread_barrier_init (barrier=0x0, attr=0x0, count=2) 在 pthread_barrier_init.c:47
47 pthread_barrier_init.c:沒有這樣的檔案或目錄。
編譯時我包含了 -lpthread 標志,并且我還確保在每個使用它的檔案中包含 pthread.h。知道為什么我的程式找不到這個 c 檔案嗎?
編輯
這是我使用的代碼片段。(這幾乎是所有代碼,但在這部分出錯了)
這是我的主回圈代碼
int main(int argc, char*argv[])
{
sbuffer_t* buffer;
sbuffer_init(&buffer);
return 0;
}
這是我的緩沖區代碼
/**
* basic node for the buffer, these nodes are linked together to create the buffer
*/
typedef struct sbuffer_node {
struct sbuffer_node *next; /**< a pointer to the next node*/
sensor_data_t data; /**< a structure containing the data */
} sbuffer_node_t;
/**
* a structure to keep track of the buffer
*/
struct sbuffer {
sbuffer_node_t *head; /**< a pointer to the first node in the buffer */
sbuffer_node_t *tail; /**< a pointer to the last node in the buffer */
pthread_rwlock_t* lock;
pthread_barrier_t* barrierRead; //Barrier to indicate that both reader threads have succesfully read the sensor reading
pthread_barrier_t* barrierWrite; //Barrier to indicate that a sensor reading has been removed
pthread_mutex_t* FIFOlock;
int finished;
};
int sbuffer_init(sbuffer_t **buffer) {
(*buffer) = malloc(sizeof(sbuffer_t));
(*buffer)->lock=malloc(sizeof(pthread_rwlock_t));
if (*buffer == NULL) return SBUFFER_FAILURE;
pthread_rwlock_init((*buffer)->lock,NULL);
pthread_rwlock_wrlock((*buffer)->lock);
pthread_barrier_init((*buffer)->barrierRead, NULL, READER_THREADS);
pthread_barrier_init((*buffer)->barrierWrite, NULL, READER_THREADS);
pthread_mutex_init((*buffer)->FIFOlock, NULL);
(*buffer)->head = NULL;
(*buffer)->tail = NULL;
(*buffer)->finished = CONNMGR_NOT_FINISHED;
pthread_rwlock_unlock((*buffer)->lock);
return SBUFFER_SUCCESS;
}
uj5u.com熱心網友回復:
錯誤不是錯誤,只是警告,它不是由您的程式發出,而是由除錯器發出。除錯器試圖通過顯示發生崩潰的源檔案來幫助您。唉,那個源檔案不是你程式的一部分,而是 pthreads 庫的一部分。由于它不可用,除錯器會通知您這一事實,否則您會希望看到出現問題的源代碼行。gdb 有一個“顯示源代碼行”函式,該函式在引發信號/例外后被呼叫,并且該函式將始終列印一些內容:源代碼行或警告訊息。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/403459.html
標籤:
