按照书上写的,不知道为什么有问题:

//已解决,参考最新的blog,哈哈

#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h> struct foo
{
int f_count;
pthread_mutex_t f_lock;
};
struct foo* AllocFoo()
{
foo* lpFoo = (foo*)malloc(sizeof(struct foo));
if(NULL != lpFoo)
{
lpFoo->f_count = 1;
if(0 != pthread_mutex_init(&lpFoo->f_lock,NULL))
{
printf("pthread_mutex_init error.\n");
free(lpFoo);
return NULL;
}
}
printf("Alloc a Foo.\n");
return lpFoo;
} bool DestoryFoo(foo* apFoo)
{
pthread_mutex_lock(&apFoo->f_lock);
if(--apFoo->f_count == 0)
{
printf("Now f_coount:%d.\n",apFoo->f_count);
pthread_mutex_unlock(&apFoo->f_lock);
pthread_mutex_destroy(&apFoo->f_lock);
free(apFoo);
printf("Destroy foo.\n");
return true;
}
else
{
printf("Now f_coount:%d.\n",apFoo->f_count);
pthread_mutex_unlock(&apFoo->f_lock);
return false;
}
} void HoldFoo(foo* apFoo)
{
pthread_mutex_lock(&apFoo->f_lock);
++apFoo->f_count;
printf("Now f_coount:%d.\n",apFoo->f_count);
pthread_mutex_unlock(&apFoo->f_lock);
} void PrintTids(const char* s); void* ThreadFun(void* Arg)
{
PrintTids("");
foo* lpFoo = (foo*)Arg;
for(int i=0;i<5;++i)
{
HoldFoo(lpFoo);
}
}
void PrintTids(const char* s)
{
pid_t lPid = getpid();
pthread_t lTid = pthread_self();
printf("%s pid:%u,tid:%u (0x%x).\n",s,(unsigned int)lPid
, (unsigned int)lTid,(unsigned int)lTid);
}
int main()
{
foo* lpFoo = AllocFoo();
pthread_t lTid = 0;
int lErr = pthread_create(&lTid,NULL,ThreadFun,NULL);
if(0 != lErr)
{
exit(1);
}
printf("main thread");
//HoldFoo(lpFoo);
bool lIsDestory = false;
do
{
lIsDestory = DestoryFoo(lpFoo);
}while(!lIsDestory);
}

  

linux线程学习的更多相关文章

  1. Linux线程学习(一)

    一.Linux进程与线程概述 进程与线程 为什么对于大多数合作性任务,多线程比多个独立的进程更优越呢?这是因为,线程共享相同的内存空间.不同的线程可以存取内存中的同一个变量.所以,程序中的所有线程都可 ...

  2. Linux线程学习(二)

    线程基础 进程 系统中程序执行和资源分配的基本单位 每个进程有自己的数据段.代码段和堆栈段 在进行切换时需要有比较复杂的上下文切换   线程 减少处理机的空转时间,支持多处理器以及减少上下文切换开销, ...

  3. Linux进程线程学习笔记:运行新程序

    Linux进程线程学习笔记:运行新程序                                         周银辉 在上一篇中我们说到,当启动一个新进程以后,新进程会复制父进程的大部份上下 ...

  4. Linux线程互斥学习笔记--详细分析

    一.互斥锁 为啥要有互斥? 多个进程/线程执行的先后顺序不确定,何时切出CPU也不确定. 多个进程/线程访问变量的动作往往不是原子的. 1. 操作步骤 (1)创建锁 // 创建互斥锁mutex pth ...

  5. Dubbo入门到精通学习笔记(十一):Dubbo服务启动依赖检查、Dubbo负载均衡策略、Dubbo线程模型(结合Linux线程数限制配置的实战分享)

    文章目录 Dubbo服务启动依赖检查 Dubbo负载均衡策略 Dubbo线程模型(结合Linux线程数限制配置的实战分享) 实战经验分享( ** 属用性能调优**): Dubbo服务启动依赖检查 Du ...

  6. Linux内核学习笔记-2.进程管理

    原创文章,转载请注明:Linux内核学习笔记-2.进程管理) By Lucio.Yang 部分内容来自:Linux Kernel Development(Third Edition),Robert L ...

  7. Linux内核学习笔记-1.简介和入门

    原创文章,转载请注明:Linux内核学习笔记-1.简介和入门 By Lucio.Yang 部分内容来自:Linux Kernel Development(Third Edition),Robert L ...

  8. Linux kernel学习-内存管理【转】

    转自:https://zohead.com/archives/linux-kernel-learning-memory-management/ 本文同步自(如浏览不正常请点击跳转):https://z ...

  9. Linux内核分析——Linux内核学习总结

    马悦+原创作品转载请注明出处+<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 Linux内核学习总结 一 ...

随机推荐

  1. 屌丝就爱尝鲜头——java8初体验

    Java8已经推出,让我们看看他的魅力.让我们看看他改变较大的部分. 一.java8概述 Java8是由Oracle(甲骨文)公司与2014年3月27日正式推出的.Java8同时推出有3套语言系统,分 ...

  2. 【Scala】Scala-使用ExecutorService-等待所有线程完成

    Scala-使用ExecutorService-等待所有线程完成 scala ExecutorService 等待_百度搜索 使用ExecutorService,如何等待所有线程完成,?_java_帮 ...

  3. 转 :scikit-learn的GBDT工具进行特征选取。

    http://blog.csdn.net/w5310335/article/details/48972587 使用GBDT选取特征 2015-03-31 本文介绍如何使用scikit-learn的GB ...

  4. Python机器学习——线性模型

    http://www.dataguru.cn/portal.php?mod=view&aid=3514 摘要 : 最近断断续续地在接触一些python的东西.按照我的习惯,首先从应用层面搞起, ...

  5. Spring(八):Spring配置Bean(一)BeanFactory&ApplicationContext概述、依赖注入的方式、注入属性值细节

    在Spring的IOC容器里配置Bean 配置Bean形式:基于xml文件方式.基于注解的方式 在xml文件中通过bean节点配置bean: <?xml version="1.0&qu ...

  6. 启动项目时出现Not a JAR.......Find JAR........一指循环就是起不来

    出现问题原因就是mapper的映射文件有问题,里面的返回类型如是实体找不到或者找重复的就会这样 解决办法就是:确保在用的实体(路径)能找到,切记不能有重名的实体

  7. oracle sqlldr使用(导入速度快,但对数据本身的处理功能弱)

    oracle sqlldr使用(导入速度快,但对数据本身的处理功能弱) 博客分类: DB.Oracle OracleSQL  sqlload.cmd pause sqlldr user/pass@tn ...

  8. SWIFT学习笔记05

    1.Swift 无需写break,所以不会发生这样的贯穿(fallthrough)的情况.2.//用不到变量名,可用"_"替换 for _ in 1...power { answe ...

  9. JAVA的堆于栈

    1. 栈(stack)与堆(heap):都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆. 2. 栈的优势:存取速度比堆要快,仅次于直接位于C ...

  10. SLF4J warning or error messages and their meanings

    来自:http://www.slf4j.org/codes.html#StaticLoggerBinder The method o.a.commons.logging.impl.SLF4FLogFa ...