互斥锁:什么是“阻塞"?意思是? [英] Mutex lock: what does "blocking" mean?

查看:359
本文介绍了互斥锁:什么是“阻塞"?意思是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读多线程和共享资源访问,互斥锁是许多(对我而言)新概念之一.我似乎无法发现的是,发现关键部分"被锁定的线程实际上正在发生什么.它说在许多地方线程被阻塞"了,但这是什么意思呢?它是否已挂起,并且在解除锁定时会恢复吗?还是会在运行循环"的下一次迭代中重试?

I've been reading up on multithreading and shared resources access and one of the many (for me) new concepts is the mutex lock. What I can't seem to find out is what is actually happening to the thread that finds a "critical section" is locked. It says in many places that the thread gets "blocked", but what does that mean? Is it suspended, and will it resume when the lock is lifted? Or will it try again in the next iteration of the "run loop"?

我问的原因是因为我想让系统提供的事件(鼠标,键盘等)(通常)在主线程上传递,并在运行循环的一个非常特定的部分中处理我的辅助线程.因此,无论传递了什么事件,我都会在自己的数据结构中排队.显然,数据结构需要一个互斥锁,因为两个线程都在修改它.缺少的难题是:当事件在主线程上的函数中传递时,我想将其排队,但队列已锁定,会发生什么?主线程会被挂起,还是会跳过锁定的部分并超出范围(丢失事件)?

The reason I ask, is because I want to have system supplied events (mouse, keyboard, etc.), which (apparantly) are delivered on the main thread, to be handled in a very specific part in the run loop of my secondary thread. So whatever event is delivered, I queue in my own datastructure. Obviously, the datastructure needs a mutex lock because it's being modified by both threads. The missing puzzle-piece is: what happens when an event gets delivered in a function on the main thread, I want to queue it, but the queue is locked? Will the main thread be suspended, or will it just jump over the locked section and go out of scope (losing the event)?

推荐答案

阻塞意味​​着执行陷入困境;通常,线程被系统置于睡眠状态,并使处理器进入另一个线程.当一个线程试图获取一个互斥锁而被阻止时,该互斥锁释放后,执行将继续执行,尽管如果另一个线程抢先获取了该互斥锁,该线程可能会再次阻塞.

Blocked means execution gets stuck there; generally, the thread is put to sleep by the system and yields the processor to another thread. When a thread is blocked trying to acquire a mutex, execution resumes when the mutex is released, though the thread might block again if another thread grabs the mutex before it can.

通常有一个尝试锁定操作,如果可能的话,该操作将获取互斥对象,否则,将返回错误.但是最终您将不得不将当前事件移到该队列中.另外,如果您延迟将事件移到处理事件的线程中,则应用程序将变得无响应.

There is generally a try-lock operation that grab the mutex if possible, and if not, will return an error. But you are eventually going to have to move the current event into that queue. Also, if you delay moving the events to the thread where they are handled, the application will become unresponsive regardless.

实际上,在不使用互斥锁的情况下,队列就可以逃脱.例如,Mac OS X(可能还有iOS)提供OSAtomicEnqueue()OSAtomicDequeue()函数(请参阅man atomic<libkern/OSAtomic.h>),这些函数利用处理器特定的原子操作来避免使用锁.

A queue is actually one case where you can get away with not using a mutex. For example, Mac OS X (and possibly also iOS) provides the OSAtomicEnqueue() and OSAtomicDequeue() functions (see man atomic or <libkern/OSAtomic.h>) that exploit processor-specific atomic operations to avoid using a lock.

但是,为什么不将处理主线程中的事件作为主运行循环的一部分呢?

But, why not just process the events on the main thread as part of the main run loop?

这篇关于互斥锁:什么是“阻塞"?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆