POSIX Threading Programming (i) Introduction

Source: Internet
Author: User
Tags posix thread

Brief introduction

In a multiprocessor architecture for shared memory, you can use threads to implement parallelism. For Unix systems, the IEEE POSIX 1003.1c Standard prescribes the standard for the C language Threading programming interface. The implementation of this standard is POSIX threads, or called pthreads.

This article begins by introducing the basic concepts of threading, motivation, and design considerations. Next comes the three main parts of the Pthreads API: thread Management, mutexes, and condition variables. Throughout this article, there is a large number of sample code to show how to use each part of the Pthread API.

Pthreads Overview

What is a thread?

From a technical standpoint, a thread is a separate instruction stream that can be run by the operating system call. But What does this mean?

From a programmer's point of view, programs that run independently of the main program can be called a thread.

Further, imagine a master program (a.out) that contains many subroutines. Then imagine that all of these subroutines can be invoked at the same time and run independently by the operating system. This is a multithreaded program.

How is this done?

Before you understand threads, understand the processes under UNIX systems. A process is created by the operating system and requires a large amount of "overhead." The process contains information about the program's resources and the execution status of the program, including:

Process ID, process group ID, user ID, and group ID

Environment

Working directory

Program Instructions

Register

Stack

Heap

File descriptor

Signal operation

Shared libraries

Process communication tools (such as Message Queuing, pipelines, semaphores, or shared memory).

UNIX Process Threads

Threads are used and exist in these process resources, and threads can be invoked by the operating system and run independently, largely because it replicates only a small amount of essential resources that exist as executable code.

A thread completes a separate control flow because it maintains its own:

Stack pointers

Register

Scheduling priority

Pending and blocking signal sets

Line Chengte Some data

So, in short, in a UNIX system environment, a thread:

exist in a process and use process resources

Always have its own independent control flow as long as the parent process exists and the operating system supports it

Replicating only the critical resources required for independent scheduling

Process resources can be shared with other threads

If the thread's parent dies, the thread dies

Is "lightweight" because most of the overhead is done when its process is created.

Sharing resources between threads of the same process, causing:

Changes made by one thread to shared system resources (such as closing a file) will be discovered by all other threads

Two pointers with the same value point to the same data

May read and write the same inner location, so programmers are required to perform explicit synchronization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.