Breaking News

Using the pthread library, you can perform low-level thread management for high-performance programming.
On Linux, you can create and manage threads in C/C++ using the POSIX thread (pthread) library. Unlike other operating systems, there is little difference between a thread and a process in Linux. That's why Linux often refers to its threads as light-weight processes.
Using the pthread library, you can create threads, wait for them to terminate, and terminate them explicitly.
Before Linux version 2.6, the main thread implementation was LinuxThreads. This implementation had significant limits in terms of performance and synchronization operations. A limit on the maximum number of threads that could run restricted them to in the 1000s.
In 2003, a team led by developers from IBM and RedHat succeeded in making the Native POSIX Thread Library (NPTL) project available. It was first introduced in RedHat Enterprise version 3 to resolve performance issues with the Java Virtual Machine on Linux. Today, the GNU C library contains implementations of both threading mechanisms.
Neither of these is an implementation of green threads, which a Virtual Machine would manage and run in purely user mode. When you use the pthread library, the kernel creates a thread each time a program starts.
You can find thread-specific information for any running process in the files under /proc/<PID>/task. This is the standard location for process information under the procfs Linux standard. For single-thread applications, it will appear that there is a task record with the same value as the PID under this directory.
Threads are like processes currently running on the operating system. In single-processor systems (e.g. microcontrollers), the operating system kernel simulates threads. This allows transactions to run simultaneously through slicing.
A single-core operating system can only really run one process at a time. However, in multi-core or multi-processor systems, these processes can run simultaneously.
You can use the pthread_create function to create a new thread. The pthread.h header file includes its signature definition along with other thread-related functions. Threads use the same address space and file descriptors as the main program.
The pthread library also includes the necessary support for mutex and conditional operations required for synchronization operations.
When you’re using the functions of the pthread library, you must ensure the compiler links the pthread library into your executable. If necessary, you can instruct the compiler to link to the library using the -l option:
The pthread_create function has the following signature:
It returns 0 if the procedure is successful. If there is a problem, it returns a non-zero error code. In the above function signature:
Here’s an example application:
When a thread returns from the main() function in an application, all threads terminate and the system frees up all resources the program used. Likewise, when exiting any thread with a command like an exit(), your program will terminate all threads.
With the pthread_join function, you can wait for a thread to terminate instead. The thread using this function will block until the expected thread terminates. The resources they use from the system are not return even in cases such as the termination of joinable threads, unscheduled by the CPU, or even failing to join with ptread_join.
Sometimes there are situations where joining with pthread_join does not make sense; if it’s impossible to predict when the thread will end, for example. In this case, you can ensure that the system returns all resources automatically at the point where the thread returns.
To achieve this, you should start the relevant threads with the DETACHED status. When starting a thread, DETACH status can be set via a thread attribute values or with the pthread_detach function:
Here’s an example use of pthread_join(). Replace the main function in the first program with the following:
When you compile and run the program, your output will be:
You can cancel a thread with a call to pthread_cancel, passing the corresponding pthread_t id:
You can see this in action in the following code. Again, only the main function is different:
Operating systems always try to run threads on one or more CPUs, either from a self-created list or from a user-created thread list. Some threads cannot run because they are waiting for an input/output signal from the hardware. They may also be waiting voluntarily, waiting for a response from another thread, or have another thread blocking them.
You can adjust the resources you allocate to threads that you create using pthread. This can be a custom scheduling policy, or you can choose scheduling algorithms such as FIFO or Round-robin if desired.
An engineer and software developer who is a fan of math and technology. He has always liked computers, mathematics and physics. He has developed game engine projects as well as machine learning, artificial neural networks and linear algebra libraries. Moreover continues to work on machine learning and linear matrices.

source

Leave a Reply

Your email address will not be published. Required fields are marked *

Share Article: