Multithreading in C, POSIX(可移植操作系统接口Portable Operating System Interface X ) style

Multithreading — An Overview

In most modern operating systems it is possible for an application to split into many "threads" that all execute concurrently(同时发生). It might not be immediately obvious why this is useful, but there are numerous(许多的) reasons why this is beneficial(有利的).

When a program is split into many threads, each thread acts like its own individual program, except that all the threads work in the same memory space, so all their memory is shared. This makes communication between threads fairly(相当的) simple, but there are a few caveats(警告说明) that will be noted later.

So, what does multithreading do for us?

Well, for starters, multiple threads can run on multiple CPUs, providing a performance improvement. A multithreaded application works just as well on a single-CPU system, but without the added speed. As multi-core processors become commonplace(普遍的), such as Dual-Core processors and Intel Pentium 4's with HyperThreading, multithreading will be one of the simplest ways to boost performance.

Secondly, and often more importantly, it allows the programmer to divide each particular job of a program up into its own piece that operates independently of all the others. This becomes particularly important when many threads are doing blocking I/O operations.

A media player, for example, can have a thread for pre-buffering the incoming media, possibly from a harddrive, CD, DVD, or network socket, a thread to process user input, and a thread to play the actual media. A stall in any single thread won't keep the others from doing their jobs.

For the operating system, switching between threads is normally cheaper than switching between processes. This is because the memory management information doesn't change between threads, only the stack and register set do, which means less data to copy on context switches.

Multithreading — Basic Concepts

Multithreaded applications often require synchronization(同步) objects. These objects are used to protect memory from being modified by multiple threads at the same time, which might make the data incorrect.

The first, and simplest, is an object called a mutex. A mutex is like a lock. A thread can lock it, and then any subsequent attempt to lock it, by the same thread or any other, will cause the attempting thread to block until the mutex is unlocked. These are very handy for keeping data structures correct from all the threads' points of view. For example, imagine a very large linked list. If one thread deletes a node at the same time that another thread is trying to walk the list, it is possible for the walking thread to fall off the list, so to speak, if the node is deleted or changed. Using a mutex to "lock" the list keeps this from happening.

Computer Scientist people will tell you that Mutex stands for Mutual Exclusion.

In Java, Mutex-like behaviour is accomplished using the synchronized keyword.

Technically speaking, only the thread that locks a mutex can unlock it, but sometimes operating systems will allow any thread to unlock it. Doing this is, of course, a Bad Idea. If you need this kind of functionality, read on about the semaphore in the next paragraph.

Similar to the mutex is the semaphore. A semaphore is like a mutex that counts instead of locks. If it reaches zero, the next attempt to access the semaphore will block until someone else increases it. This is useful for resource management when there is more than one resource, or if two separate(分离) threads are using the same resource in coordination. Common terminology(术语) for using semaphores is "uping" and "downing", where uping increases the count and downing decreases and blocks on zero.

Java provides a Class called Semaphore which does the same thing, but uses acquire() and release() methods instead of uping and downing.

With a name as cool-sounding as semaphore, even Computer Scientists couldn't think up what this is short for. (Yes, I know that a semaphore is a signal or flag

最新文章

  1. 用wget命令下载jdk
  2. ubuntu nginx 安装以及配置文件详解
  3. tomcat启动报错:IOException while loading persisted sessions: java.io.EOFException.
  4. 加一个 时间戳 TimeStamp 可以解决 重复提交问题 SqlServer
  5. Linux(CentOS 5.5) Redis安装
  6. POJ 1845
  7. idea中output log4j中文乱码
  8. Solr DateRangeField
  9. Customer segmentation – LifeCycle Grids with R(转)
  10. salesforce零基础学习(七十三)ProcessInstanceWorkItem/ProcessInstanceStep/ProcessInstanceHistory浅谈
  11. (整理4)RPC服务和HTTP服务简单说明
  12. python同步、互斥锁、死锁
  13. Cisco NTP配置
  14. centos7 安装 nvm
  15. Magento模型与ORM基础
  16. Chinese Seals
  17. Python数值运算与赋值的快捷方式
  18. win7 下jenkins配置与使用
  19. pthread_cond_wait()函数的详解
  20. BZOJ 2118 墨墨的等式(最短路)

热门文章

  1. Yii Model
  2. HDU - 4676 :Sum Of Gcd (莫队&区间gcd公式)
  3. BZOJ1718: [Usaco2006 Jan] Redundant Paths 分离的路径【边双模板】【傻逼题】
  4. asp.net core microservices 架构之分布式自动计算(三)-kafka日志同步至elasticsearch和kibana展示
  5. 《DSP using MATLAB》Problem 3.1
  6. 【idea】如何破解idea
  7. streamsets Processors 说明
  8. 显示列表控件(引用SourceGrid)
  9. Ubuntu14.04下Sublime Text 3解决无法输入中文
  10. SQL中禁用trigger