Advanced Programming in UNIX Environment Episode 52
发布日期:2021-10-07 23:47:34 浏览次数:1 分类:技术文章

本文共 2208 字,大约阅读时间需要 7 分钟。

Mutexes

A mutex variable is represented by the pthread_mutex_t data type. Before we can use a mutex variable, we must first initialize it by either setting it to the constant PTHREAD_MUTEX_INITIALIZER (for statically allocated mutexes only) or calling pthread_mutex_init. If we allocate the mutex dynamically (by calling malloc, for example), then we need to call pthread_mutex_destroy before freeing the memory.

#include 
int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict);int pthread_mutex_destroy(pthread_mutex_t *mutex);

To lock a mutex, we call pthread_mutex_lock. If the mutex is already locked, the calling thread will block until the mutex is unlocked. To unlock a mutex, we call pthread_mutex_unlock.

#include 
int pthread_mutex_lock(pthread_mutex_t *mutex);int pthread_mutex_trylock(pthread_mutex_t *mutex);int pthread_mutex_unlock(pthread_mutex_t *mutex);
#include 
#include
struct foo{ int f_count; pthread_mutex_t f_lock; int f_id;};struct foo* foo_alloc(int id){ struct foo *fp; if((fp=malloc(sizeof(struct foo))) { fp->f_count=1; fp->f_id=id; if(pthread_mutex_init(&fp->f_lock,NULL)!=NULL) { free(fp); return NULL; } } return fp;}void foo_hold(struct foo *fp){ pthread_mutex_lock(&fp->f_lock); if(--fp->f_count==0) { pthread_mutex_unlock(&fp->f_lock); pthread_mutex_destroy(&fp->f_lock); free(fp); } else { pthread_mutex_unlock(&fp->f_lock); }}

Using a mutex to protect a data structure

Deadlock Avoidance

A thread will deadlock itself if it tries to lock the same mutex twice, but there are less obvious ways to create deadlocks with mutexes.

Deadlocks can be avoided by carefully controlling the order in which mutexes are locked.

Sometimes, an application’s architecture makes it difficult to apply a lock ordering. If enough locks and data structures are involved that the functions you have available can’t be molded to fit a simple hierarchy, then you’ll have to try some other approach. In this case, you might be able to release your locks and try again at a later time.

转载地址:https://blog.csdn.net/myfather103/article/details/79823324 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Advanced Programming in UNIX Environment Episode 53
下一篇:Advanced Programming in UNIX Environment Episode 51

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月08日 04时55分26秒