Programming with POSIX® Threads

David R. Butenhof

出版时间

1997-05-26

ISBN

9780201633924

评分

★★★★★
书籍介绍

With this practical book, you will attain a solid understanding of threads and will discover how to put this powerful mode of programming to work in real-world applications. The primary advantage of threaded programming is that it enables your applications to accomplish more than one task at the same time by using the number-crunching power of multiprocessor parallelism and by automatically exploiting I/O concurrency in your code, even on a single processor machine. The result: applications that are faster, more responsive to users, and often easier to maintain. Threaded programming is particularly well suited to network programming where it helps alleviate the bottleneck of slow network I/O. This book offers an in-depth description of the IEEE operating system interface standard, POSIXAE (Portable Operating System Interface) threads, commonly called Pthreads. Written for experienced C programmers, but assuming no previous knowledge of threads, the book explains basic concepts such as asynchronous programming, the lifecycle of a thread, and synchronization. You then move to more advanced topics such as attributes objects, thread-specific data, and realtime scheduling. An entire chapter is devoted to "real code," with a look at barriers, read/write locks, the work queue manager, and how to utilize existing libraries. In addition, the book tackles one of the thorniest problems faced by thread programmers-debugging-with valuable suggestions on how to avoid code errors and performance problems from the outset. Numerous annotated examples are used to illustrate real-world concepts. A Pthreads mini-reference and a look at future standardization are also included.

AI导读
核心看点
  • 深入解析POSIX线程标准接口
  • 详解线程同步与资源管理机制
  • 提供多线程编程实战最佳实践
适合谁读
  • Linux系统下的多线程开发者
  • 需要掌握并发编程的程序员
  • 网络编程及高性能应用开发者
读前提醒
  • 建议结合UNP系列书籍对照阅读
  • 代码讲解较简略,需自行琢磨
  • 注意部分翻译可能不够通顺
读者共识
  • 内容全面准确,属必读经典
  • 适合入门了解,类似字典查阅
  • 部分隐含逻辑需读者自行推导

本导读基于书籍简介、目录、原文摘录、短评和书评生成,不等同于全文精读。

精彩摘录
  • "有关线程创建最重要的是,在当前线程从函数pthread_create中返回以及新线程被调度执行之间不存在同步关。即,新线程可能在当前线程从pthread_create返回之前就运行了。甚至在当前线程从pthread_create返回之前,新线程就可能已经运行完毕了。"
  • "线程终止后线程至少保留了线程ID和void* 返回值。该返回值从线程启动函数中返回或者在pthread_exit调用时设定。通过函数返回或者pthread_exit调用正常终止的线程与通过取消调用终止的线程见的唯一外部区别是:被取消的线程其返回值总是PTHREAD_CANCELLED"
  • "终止线程将释放所有系统资源,但你必须释放由该线程占有的程序资源。调用malloc或mmap分配的内存可以在任何时候、由任何线程释放。互斥量、条件变量和信号灯可以由任何线程销毁,只要它们被解锁并没有线程等待。但是,只有互斥量的主人能够解锁它。如果线程终止时还有加锁的互斥量,则该互斥量就不能被再次使用。因为不会被解锁"
  • "有很多理由解释在线程醒来时检测谓词是否为真是个不错的主题"
  • "通常只需要几条指令来重测谓词,而且这是个良好的编程规则。如果不重新检测谓词,可能导致后来难以跟踪的严重错误。"
  • "相对于其他覆盖相同32或64位数据单元的内存操作而言,8位写操作不是原子操作。大部分计算机会写包含修改数据的整个内存数据单元(如32位)。如果两个线程向同一个32位内存单元写入不同的8位数据,则结果是最后写入的线程将确定两个8位字节的值,即覆盖第一个线程写入的值。"
  • "使用pthread_once的主要原因是原来不能静态地初始化一个互斥量。为了使用一个互斥量,不得不首先调用pthread_mutex_init初始化互斥量。必须仅仅初始化互斥量一次,因此初始化调用应在一次性初始化代码中进行。 pthread_once解决了这个递归的问题"
  • "POSIX为线程创建定义了下列属性:detachstate、stacksize、stackaddr、scope、inheritsched、schedpolicy和schedparam。一些系统不会支持所有这些属性,因此需要在使用他们前检查系统文档。"
用户评论
字典式样的书,适合入门了解
必读经典, 可以参考web上的相关网站
未读完,不作评。
This book is informative and accurate, should be a must-read for pthread programmers. But there's one thing I don't like: too many implications. The author should spell his claims more loudly and explicitly.
unp引荐的书,书中介绍了各种线程同步技术,及多线程使用场景举例。再结合《unp2》看,功效更好
收藏