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

//已解决,参考最新的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. 使用 GNU Libtool 创建库

    这篇文档向大家介绍 GNU Libtool 的用途及基本使用方法,同时描述如何结合 GNU Autoconf 和 Automake 来使用 Libtool. 3 评论: 吴 小虎, 程序员, 天用唯勤 ...

  2. linux kernel内存映射实例分析

    作者:JHJ(jianghuijun211@gmail.com)日期:2012/08/24 欢迎转载,请注明出处 引子 现在android智能手机市场异常火热,硬件升级非常迅猛,arm cortex ...

  3. free的说明

    http://www.cnblogs.com/peida/archive/2012/12/25/2831814.html

  4. FPS游戏服务器设计的问题 【转】

    一.追溯 去gameloft笔试,有一个题目是说: 叫你去设计一个FPS(第一人称射击游戏),你是要用TCP呢还是要用UDP,说明理由 . 二.学习 这是两篇网上找到的文章,写非常不错. 当时笔试的时 ...

  5. Direct2D教程II——绘制基本图形和线型(StrokeStyle)的设置详解

    目前,在博客园上,相对写得比较好的两个关于Direct2D的教程系列,分别是万一的Direct2D系列和zdd的Direct2D系列.有兴趣的网友可以去看看.本系列也是介绍Direct2D的教程,是基 ...

  6. 怎样让孩子爱上设计模式 —— 7.适配器模式(Adapter Pattern)

    怎样让孩子爱上设计模式 -- 7.适配器模式(Adapter Pattern) 标签: 设计模式初涉 概念相关 定义: 适配器模式把一个类的接口变换成client所期待的还有一种接口,从而 使原本因接 ...

  7. Android实战技巧:如何在ScrollView中嵌套ListView

    前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个ListItem中放入另外一个ListView.但刚开始的时候,会发现放入的小ListVie ...

  8. 【SCM】关于Gradle与maven的几篇文章

    Gradle官方文档:https://docs.gradle.org/current/userguide/installation.html#sec:download 使用 Gradle 命令行:ht ...

  9. Linux下安装配置SNMP服务

    一.安装snmp服务 1.检查系统是否已经安装snmp的rpm包 以下是安装snmp服务需要的rpm包: libsensors3-2.10.6-55.el5.i386.rpm lm_sensors-2 ...

  10. 使用Json.Net解决MVC中各种json操作

    最近收集了几篇文章,用于替换MVC中各种json操作,微软mvc当然用自家的序列化,速度慢不说,还容易出问题,自定义性也太差,比如得特意解决循环引用的问题,比如datetime的序列化格式,比如性能. ...