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 solve the problem of deadlock and read-write lock in C language multithreaded development

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

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how to solve the problem of deadlock and read-write lock in C language multithreaded development. the content is detailed and the logic is clear. I believe most people still know too much about this knowledge. so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Deadlock

Sometimes a thread needs to access two or more different shared resources at the same time, and each resource is managed by a different mutex. Deadlocks may occur when more than one thread locks the same set of mutexes

Two or more processes in the implementation process, due to the competition for shared resources caused by the phenomenon of mutual waiting, if there is no external force, they will not be able to move forward. At this point, it is said that the system is in a deadlock state or the system has a deadlock.

Several scenarios of deadlock:

Forgot to release the lock.

Repeat locking (repeatedly adding the same lock)

Multi-thread and multi-lock to preempt lock resources

/ / Multi-thread and multi-lock, preempt lock resources # include # include # include / / create two mutexes pthread_mutex_t mutex1, mutex2;void * workA (void * arg) {pthread_mutex_lock (& mutex1); sleep (1); pthread_mutex_lock (& mutex2); printf ("workA....\ n"); pthread_mutex_unlock (& mutex2); pthread_mutex_unlock (& mutex1); return NULL } void * workB (void * arg) {pthread_mutex_lock (& mutex2); sleep (1); pthread_mutex_lock (& mutex1); printf ("workB....\ n"); pthread_mutex_unlock (& mutex1); pthread_mutex_unlock (& mutex2); return NULL;} int main () {/ / initialize pthread_mutex_init (& mutex1, NULL); pthread_mutex_init (& mutex2, NULL) / / create 2 child threads pthread_t tid1, tid2; pthread_create (& tid1, NULL, workA, NULL); pthread_create (& tid2, NULL, workB, NULL); / / recover child thread resources pthread_join (tid1, NULL); pthread_join (tid2, NULL); / / release mutually exclusive resources pthread_mutex_destroy (& mutex1); pthread_mutex_destroy (& mutex2); return 0;}

Execution result:

Read-write locks / * types of read-write locks pthread_rwlock_t int pthread_rwlock_init (pthread_rwlock_t * restrict rwlock, const pthread_rwlockattr_t * restrict attr); int pthread_rwlock_destroy (pthread_rwlock_t * rwlock); int pthread_rwlock_rdlock (pthread_rwlock_t * rwlock); int pthread_rwlock_tryrdlock (pthread_rwlock_t * rwlock); int pthread_rwlock_wrlock (pthread_rwlock_t * rwlock) Int pthread_rwlock_trywrlock (pthread_rwlock_t * rwlock); int pthread_rwlock_unlock (pthread_rwlock_t * rwlock); case: eight threads operate on the same global variable. Three threads write the global variable irregularly, and five threads read the global variable * / # include # include # include / / to create a shared data int num = 1 pthread_rwlock_wrlock * writeNum (void * arg) {while (1) {pthread_rwlock_wrlock (& rwlock); num++ Printf ("+ + write, tid:% ld, num:% d\ n", pthread_self (), num); pthread_rwlock_unlock (& rwlock); usleep (100);} return NULL;} void * readNum (void * arg) {while (1) {pthread_rwlock_rdlock (& rwlock); printf ("= = read, tid:% ld, num:% d\ n", pthread_self (), num) Pthread_rwlock_unlock (& rwlock); usleep (100);} return NULL;} int main () {pthread_rwlock_init (& rwlock, NULL); / / create 3 writer threads, 5 reader threads pthread_t wtids [3], rtids [5]; for (int I = 0; I < 3; iTunes +) {pthread_create (& wtids [I], NULL, writeNum, NULL) } for (int I = 0; I < 5; iTunes +) {pthread_create (& rtids [I], NULL, readNum, NULL);} / / set thread separation for (int I = 0; I < 3; iSplus +) {pthread_detach (wtids [I]);} for (int I = 0; I < 5; iSense +) {pthread_detach (rtids [I]);} pthread_exit (NULL) Pthread_rwlock_destroy (& rwlock); return 0;}

Execution result:

Share when reading, improve efficiency compared with mutex.

These are all the contents of this article entitled "how to solve the problem of deadlock and read-write lock in C language multithreaded development". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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