Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a Pthread?

I'm getting confused on the idea of "pthread" and "thread". I know pthread is short form for POSIX thread, which is a type of standardized thread used in UNIX. But people often use "thread" to refer a thread. Are pthread and thread equivalent? Or pthread is only the name for threads used in UNIX? Thanks in advance.

like image 470
turtlesoup Avatar asked Feb 28 '13 04:02

turtlesoup


People also ask

What does pthread stand for?

PThreads is a highly concrete multithreading system that is the UNIX system's default standard. PThreads is an abbreviation for POSIX threads, and POSIX is an abbreviation for Portable Operating System Interface, which is a type of interface that the operating system must implement.

How do pthreads work?

Pthread uses sys_clone() to create new threads, which the kernel sees as a new task that happens to share many data structures with other threads. To do synchronization, pthread relies heavily on futexes in the kernel.

Is pthread still used?

Yes, the pthreads library is still used for threading.

Can we use pthread in C++?

In a Unix/Linux operating system, the C/C++ languages provide the POSIX thread(pthread) standard API(Application program Interface) for all thread related functions. It allows us to create multiple threads for concurrent process flow.


2 Answers

Threads are a generic concept. Wikipedia defines it as:

In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by an operating system scheduler. A thread is a light-weight process.

Pthreads or POSIX threads are one implementation of that concept used with C program on Unix. Most modern languages have their own implementation of threads. From that web page:

Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h header/include file and a thread library - though this library may be part of another library, such as libc, in some implementations.

like image 151
Gray Avatar answered Sep 28 '22 10:09

Gray


To add to Gray,

Pthread is POSIX complaint which means you can use it across most of the UNIX operating systems.

No need to rewrite them for each of Unix (Linxux,FreeBSD, etc) and the behavior would be same across all of them .

like image 41
Pradheep Avatar answered Sep 28 '22 10:09

Pradheep