源代码:

#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

线程异常:undefined reference to &#39;pthread_create&#39; 处理的更多相关文章

  1. (笔记)Linux线程编译undefined reference to 'pthread_create'

    在使用线程时,使用gcc或arm-linux-gcc编译时,会出现错误:undefined reference to 'pthread_create' 主要是以下两种原因: 1.#include &l ...

  2. 在linux下编译线程程序undefined reference to `pthread_create'

    由于是Linux新手,所以现在才开始接触线程编程,照着GUN/Linux编程指南中的一个例子输入编译,结果出现如下错误:undefined reference to 'pthread_create'u ...

  3. openCV中 libopencv-nonfree-dev的安装: undefined reference to `cv::initModule_nonfree()&#39;

    今天照着一起做RGB-D SLAM (3)    , 程序会出现以下的错误: cv::initModule_nonfree(); /home/yhzhao/slam/src/detectFeature ...

  4. undefined reference to symbol 'pthread_create@@GLIBC_2.2.5' 的修改方法

    在编译DSO代码的时候会如下这样的问题: 检查DSO,在程序中没有用到pthread,但是在编译的时候却出现此类问题.仔细想了想了一下,在程序中用到了C++11中的线程std::thread,个人猜测 ...

  5. undefined reference to symbol' pthread_create@@GLIBC_2.2.5'

    我在ubuntu16.04上迁移工程,遇到了这个错误. pthread库不是Linux系统默认的库,链接时需要添加-pthread参数. 这里注意是链接那一步添加-pthread,而不是编译选项.

  6. linux下开发,解决cocos2d-x中编译出现的一个小问题, undefined reference to symbol &#39;pthread_create@@GLIBC_2.2.5&#39;

    解决cocos2d-x中编译出现的一个小问题 对于cocos2d-x 2.×中编译中,若头文件里引入了#include "cocos-ext.h",在进行C++编译的时候会遇到例如 ...

  7. Linux Ubuntu运行线程程序出现undefined reference to ‘pthread_create’和undefined reference to ‘pthread_join’错误。

    Linux Ubuntu运行线程程序出现undefined reference to ‘pthread_create’和undefined reference to ‘pthread_join’错误. ...

  8. 问题:eclipse中线程编程编译报错,undefined reference to 'pthread_create'的解决方法(已解决)

    问题描述: 在Ubuntu系统中,使用eclipse CDT集成开发环境编写pthread程序,编译时,pthread_create不通过,报错信息是: undefined reference to ...

  9. undefined reference to `sin&#39;问题解决

    作者:zhanhailiang 日期:2014-10-25 使用gcc编译例如以下代码时报"undefined reference to `sin'": #include < ...

随机推荐

  1. asp.net 5.0微信支付

    (原文出自:http://lib.csdn.net/article/wechat/46329) 微信支付官方坑太多,我们来精简 我把官方的代码,打包成了 an.wxapi.dll. 里面主要替换了下注 ...

  2. (七)《Java编程思想》——多态的缺陷

    1.不能“覆盖”私有方法 package chapter8; /** * 不能"覆盖"私有方法 */ public class PrivateOverride { private ...

  3. Sql时间函数

    一.sql server日期时间函数 Sql Server中的日期与时间函数  1.  当前系统日期.时间   select getdate()    2. dateadd  在向指定日期加上一段时间 ...

  4. MongoDB-C# Driver账户密码登录问题

    MongoDb在3.0之后添加了SCRAM-SHA-1,用户验证模式.添加的用户,默认登录协议也是这个. 在登陆的时候就要选择使用这种方式登录.有的gui客户端的登录验证方式还是MONGODB-CR. ...

  5. ZOJ3551 Bloodsucker(概率dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Bloodsucker Time Limit: 2 Seconds      Me ...

  6. mysql foreign key 外键

    ALTER TABLE `fd_rel_customer_doctor` ADD CONSTRAINT `FK_fd_rel_customer_doctor_1` FOREIGN KEY (`CUST ...

  7. MYSQL常见出错mysql_errno()代码解析

    如题,今天遇到怎么一个问题, 在理论上代码是不会有问题的,但是还是报了如上的错误,把sql打印出來放到DB中却可以正常执行.真是郁闷,在百度里面 渡 了很久没有相关的解释,到时找到几个没有人回复的 & ...

  8. kakfa-性能相关

    1.增大partition最大连接数 kafka的集群有多个Broker服务器组成,每个类型的消息被定义为topic,同一topic内部的消息按照一定的key和算法被分区(partition)存储在不 ...

  9. laravel跟jquery之间传输json数据

    laravel代码: public function test(){ $arr = ["test1"=>"1","test2"=> ...

  10. 基于htmlparser实现网页内容解析

    基于htmlparser实现网页内容解析 网页解析,即程序自动分析网页内容.获取信息,从而进一步处理信息. 网页解析是实现网络爬虫中不可缺少而且十分重要的一环,由于本人经验也很有限,我仅就我们团队开发 ...