線程異常:undefined reference to 'pthread_create' 處理
源碼:
#include <stdio.h>
#include <pthread.h>
#include <sched.h>
void *producter_f (void *arg);
void *consumer_f (void *arg);
int buffer_has_item=0;
pthread_mutex_t mutex;
int running =1 ;
int main (void)
{
pthread_t consumer_t;
pthread_t producter_t;
pthread_mutex_init (&mutex,NULL);
pthread_create(&producter_t, NULL,(void*)producter_f, NULL );
pthread_create(&consumer_t, NULL, (void *)consumer_f, NULL);
usleep(1);
running =0;
pthread_join(consumer_t,NULL);
pthread_join(producter_t,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
void *producter_f (void *arg)
{
while(running)
{
pthread_mutex_lock (&mutex);
buffer_has_item++;
printf("生產,總數量:%d\n",buffer_has_item);
pthread_mutex_unlock(&mutex);
}
}
void *consumer_f(void *arg)
{
while(running)
{
pthread_mutex_lock(&mutex);
buffer_has_item--;
printf("消費,總數量:%d\n",buffer_has_item);
pthread_mutex_unlock(&mutex);
}
}
錯誤場景:
[root@luozhonghua 04]# gcc -o mutex ex04-5-mutex.c
/tmp/ccZuFiqr.o: In function `main':ex04-5-mutex.c:(.text+0x3d): undefined reference to `pthread_create'
ex04-5-mutex.c:(.text+0x61): undefined reference to `pthread_create'
ex04-5-mutex.c:(.text+0x8b): undefined reference to `pthread_join'
ex04-5-mutex.c:(.text+0x9f): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
分析:pthread 庫不是 Linux 係統默認的庫,連接時需要使用靜態庫 libpthread.a
處理:
在編譯中加 -lpthread 參數
[root@luozhonghua 04]# gcc -lpthread -o mutex ex04-5-mutex.c
最後更新:2017-04-03 05:39:56
上一篇:
我的Java學習推薦書目
下一篇:
Redis數據清除問題
IE,firefox下使用CSS屬性overflow的存在的不同
人人商城V3 3.1.3 完整版 微擎微讚通用模塊優化修複V3版本
[usaco]羅馬數字
ORACLE uuid自動生成主鍵
數據結構開場白
D-News | “雲計算與大數據”重點專項2017年度第二次總體專家組會議在京召開
Design and Application Learning of the Distributed Call Tracing System
理解Storm的內部消息緩衝機製
Android 發送HTTP GET POST 請求以及通過 MultipartEntityBuilder 上傳文件(二)
Java網絡編程之TCP粘包拆包