简体   繁体   English

如何避免互斥锁阻塞?

[英]How to avoid mutex_lock from blocking?

I want to use threads with pthread in C. 我想在C中将线程与pthread一起使用。

One question I have about threads is why mutex lock does not block the execution when the whole application relays on one shared ressource? 关于线程的一个问题是,为什么当整个应用程序在一个共享资源上进行中继时,互斥锁不会阻止执行?

Bodo 博多

You want the pthread_mutex_trylock() function. 您需要pthread_mutex_trylock()函数。

The pthread_mutex_trylock() function shall be equivalent to pthread_mutex_lock(), except that if the mutex object referenced by mutex is currently locked (by any thread, including the current thread), the call shall return immediately. pthread_mutex_trylock()函数应与pthread_mutex_lock()等效,不同之处在于,如果当前由互斥锁引用的互斥对象被锁定(任何线程(包括当前线程)),则调用应立即返回。

Reference: https://computing.llnl.gov/tutorials/pthreads/man/pthread_mutex_trylock.txt 参考: https : //computing.llnl.gov/tutorials/pthreads/man/pthread_mutex_trylock.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM