https://computing.llnl.gov/tutorials/pthreads/

  • Technically, a thread is defined as an independent stream of instructions that can be scheduled to run as such by the operating system. But what does this mean?
  • To the software developer, the concept of a "procedure" that runs independently from its main program may best describe a thread.
  • To go one step further, imagine a main program (a.out) that contains a number of procedures. Then imagine all of these procedures being able to be scheduled to run simultaneously and/or independently by the operating system. That would describe a "multi-threaded" program.
  • How is this accomplished?
  • Before understanding a thread, one first needs to understand a UNIX process. A process is created by the operating system, and requires a fair amount of "overhead". Processes contain information about program resources and program execution state, including:

    • Process ID, process group ID, user ID, and group ID
    • Environment
    • Working directory.
    • Program instructions
    • Registers
    • Stack
    • Heap
    • File descriptors
    • Signal actions
    • Shared libraries
    • Inter-process communication tools (such as message queues, pipes, semaphores, or shared memory).
    UNIX PROCESS THREADS WITHIN A UNIX PROCESS
  • Threads use and exist within these process resources, yet are able to be scheduled by the operating system and run as independent entities largely because they duplicate only the bare essential resources that enable them to exist as executable code.
  • This independent flow of control is accomplished because a thread maintains its own:
    • Stack pointer
    • Registers
    • Scheduling properties (such as policy or priority)
    • Set of pending and blocked signals
    • Thread specific data.
  • So, in summary, in the UNIX environment a thread:
    • Exists within a process and uses the process resources
    • Has its own independent flow of control as long as its parent process exists and the OS supports it
    • Duplicates only the essential resources it needs to be independently schedulable
    • May share the process resources with other threads that act equally independently (and dependently)
    • Dies if the parent process dies - or something similar
    • Is "lightweight" because most of the overhead has already been accomplished through the creation of its process.
  • Because threads within the same process share resources:
    • Changes made by one thread to shared system resources (such as closing a file) will be seen by all other threads.
    • Two pointers having the same value point to the same data.
    • Reading and writing to the same memory locations is possible, and therefore requires explicit synchronization by the programmer.

What is a Thread?的更多相关文章

  1. 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例

    前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...

  2. 记一次tomcat线程创建异常调优:unable to create new native thread

    测试在进行一次性能测试的时候发现并发300个请求时出现了下面的异常: HTTP Status 500 - Handler processing failed; nested exception is ...

  3. 多线程爬坑之路-Thread和Runable源码解析

    多线程:(百度百科借一波定义) 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提 ...

  4. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

    学习架构探险,从零开始写Java Web框架时,在学习到springAOP时遇到一个异常: "C:\Program Files\Java\jdk1.7.0_40\bin\java" ...

  5. Exception in thread "main" java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V

    在学习CGlib动态代理时,遇到如下错误: Exception in thread "main" java.lang.NoSuchMethodError: org.objectwe ...

  6. Thread.Sleep(0) vs Sleep(1) vs Yeild

    本文将要提到的线程及其相关内容,均是指 Windows 操作系统中的线程,不涉及其它操作系统. 文章索引 核心概念 Thread.Yeild       Thread.Sleep(0) Thread. ...

  7. Android笔记——Handler Runnable与Thread的区别

    在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...

  8. Java Thread 的 sleep() 和 wait() 的区别

    Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别       1. sleep ...

  9. Android线程管理之Thread使用总结

    前言 最近在一直准备总结一下Android上的线程管理,今天先来总结一下Thread使用. 线程管理相关文章地址: Android线程管理之Thread使用总结 Android线程管理之Executo ...

  10. C# - 多线程 之 Process与Thread与ThreadPool

    Process 进程类, // 提供对本地和远程进程的访问,启动/停止本地系统进程 public class Process : Component { public int Id { get; } ...

随机推荐

  1. Android 通过网络获取图片的源码

    将开发过程中经常用到的内容做个备份,如下的资料是关于Android 通过网络获取图片的的内容. package com.netimg; import android.app.Activity;impo ...

  2. Sqlite CodeFirst的初级实现

    示例实体: using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnn ...

  3. 字符型图片验证码,使用tensorflow实现卷积神经网络,进行验证码识别CNN

    本项目使用卷积神经网络识别字符型图片验证码,其基于 TensorFlow 框架.它封装了非常通用的校验.训练.验证.识别和调用 API,极大地减低了识别字符型验证码花费的时间和精力. 项目地址: ht ...

  4. Linux——【rpm、yun、源码包】安装

    RPM包或者安装源码包 在windows下安装一个软件很轻松,只要双击.exe的文件,安装提示连续“下一步”即可,然而linux系统下安装一个软件似乎并不那么轻松,因为我们不是在图形界面下.所以我们要 ...

  5. 5. AQS(AbstractQueuedSynchronizer)抽象的队列式的同步器

    5.1 AbstractQueuedSynchronizer里面的设计模式--模板模式 模板模式:父类定义好了算法的框架,第一步做什么第二步做什么,同时把某些步骤的实现延迟到子类去实现. 5.1.1 ...

  6. js 省份城市二级动态联动的例子

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. C#请求http向网页发送数据,网页接收

    首先,我们需要的是什么东西? 用POST方式请求http,给网页传输数据,网页接收到数据之后,把数据存储到数据库中. 1.首先请求http,建立连接,把转码过的数据传输过去 2.网页接收数据,在转码之 ...

  8. java冒泡排序 常规排序和优化排序

    冒泡排序原理在于两两比较,看你需要把大值放最前面还是最后面, 我们看看把大值放在后面:比如数组[7, 5, 6] 开始排序第1个数字 :7 arr:[7, 5, 6] 开始排序第2个数字 :5 arr ...

  9. HTML代码中<%%>、<%=%>、<%:%>

    <%%>之间可以写服务器端代码 比如 <% for(var i=0;i<10;i++){%> <%=%>获取后台的变量值,比如后台一个session[&quo ...

  10. C# 用tabcontrol实现窗体类似网页排版的显示

    这里做的比较简陋,可以美化下 把form设置为非顶级控件,直接放在tabcontrol里边,然后实现tabcontrol的拖拽移除tabpage显示form以及添加tabpage mousemove的 ...