Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create Multithreading in ​ linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to create multithreading in linux related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this linux article on how to create multithreading, let's take a look at it.

Linux threads are divided into two categories: core-level supporting threads and user-level threads. It is generally a user-level thread.

1. Several common functions of multithreading

To create multithreading, you must load the pthread.h file, the library file pthread. The identifier of the thread pthread_t is defined in the header file / usr/include/bits/pthreadtypes.h: typedef unsigned long int pthread_t

1. Create a thread:

Int pthread_create (pthread_t * restrict thread

Const pthread_attr_t * restrict attr

Void* (* start_routine) (void*), void* restrict arg)

Parameters:

Thread output thread id

Attr thread property, default null

The start_ route thread executes the function

Arg thread execution parameters

Note: function returns 0 successfully, otherwise returns error code

two。 Wait for the specified thread to finish:

Int pthread_join (pthread_t thread,void * * value_ptr)

Parameters:

Thread A valid thread id

Value_ptr receives a pointer to the value returned by the thread

Note: the thread calling this function will be in a suspended state or return directly with an error before the specified thread exits. If value_ptr is not null, value_ptr points to the thread's return value. After the function succeeds, the resources used by the specified thread will be released.

3. Exit thread:

Int pthread_exit (void * value_ptr)

Parameters:

The value_ptr thread returns a value pointer

Note: ptrhead_exit () exits the thread calling this function and releases the resources consumed by that thread.

4. Get the current thread id:

Pthread_t pthread_self (void)

Parameters:

Note: returns the id of the current function

5. Mutual exclusion

Create a mutex:

Int pthread_mutex_init (pthread_mutex_t * restrict mutex

Const pthread_mutexattr_t * restrict attr)

Parameters:

Mutex output mutex id

Attr mutex attribute, default null

Note: function returns 0 successfully, otherwise returns error code

Lock the mutex:

Int pthread_mutex_lock (pthread_mutex_t * mutex)

Parameters:

Mutex mutually exclusive id

Note: if the specified mutex id is locked, the calling thread will remain suspended until the mutex id is completely unlocked, otherwise the mutex will be locked.

Int pthread_mutex_trylock (pthread_mutex_t * mutex)

Parameters:

Mutex mutually exclusive id

Note: if the specified mutex id is locked, an error will be returned directly, which will be handled differently by judging the error. Pthread_mutex_trylock is similar to pthread_mutex_lock, except that pthread_mutex_trylock blocks only if the mutex is locked.

Unlock mutex:

Int pthread_mutex_unlock (pthread_mutex_t * mutex)

Parameters:

Mutex mutually exclusive id

Note: unlock the specified mutex id if it is already locked

Release mutex:

Int pthread_mutex_destroy (pthread_mutex_t * mutex)

Parameters:

Mutex mutually exclusive id

Note: releases the resources consumed by the specified mutex.

The functions pthread_mutex_init and pthread_mutex_destroy are the constructor and destructor of the mutex, respectively.

Second, multi-thread synchronization

1. Repulsive body

A mutex (mutex) is the equivalent of a lock and guarantees the following three points:

◎ atomicity: if a thread locks a mutex, either all or none of the operations in the critical zone are performed.

◎ uniqueness: if a thread locks a mutex, no other thread can lock the mutex until it is unlocked.

◎ non-busy waiting: if one thread has locked a mutex and the second thread tries to lock the mutex, the second thread will be suspended (without consuming any cpu resources) until the first thread unlocks the mutex.

two。 Conditional variable

A conditional variable is a mechanism that enables a thread (without consuming cpu) to wait for certain events to occur. Some threads may wait for a condition variable until some other thread sends a signal to the condition variable, and one of these threads wakes up to handle the event. However, the condition variable does not provide locking, so it must be used in conjunction with a mutex to provide the necessary locking to access the environment variable.

3. Semaphore

Dijkstra put forward the concept of semaphore. Semaphore is a special variable, which can only take positive integer values. There are only two operations for this positive integer: P operation (for waiting, off operation) and v operation (for signal, open operation).

The pbrow operation is defined as follows (suppose we have a semaphore sem):

P (sem): if the value of sem is greater than 0, sem is minus 1; if the value of sem is 0, the thread is suspended.

V (sem): if another process hangs while waiting for sem, let it resume execution; if no thread is suspended waiting for sem, sem plus 1.

Creation and opening of signal set

Int semget (key_t key,int nsems,int flag)

Operation of semaphore

Int semop (int semid,struct sembuf semoparray [], size_t nops)

Control of semaphore

Int semctl (int semid,int semnum int cmd,union semun arg)

PS: the classic producer-consumer problem (producer-costomer) is a famous synchronization problem.

This is the end of the article on "how to create multithreading in linux". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to create multithreading in linux". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report