且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不同步时钟的情况下对线程强制执行线程锁定(objective-c)

更新时间:2022-03-26 22:20:07

为什么要动态分配锁对象?当广播者线程通过时,没有锁定对象,因此它不会锁定它.然后,该锁被分配并锁定在通信线程中(无法告诉广播电台已经进入其临界区域),并且两者并行运行.

Why are you allocating the lock object dynamically? When the broadcaster thread goes through there's no lock object, so it doesn't lock it. Then the lock is allocated and locked in the communicator thread (which can't tell the broadcaster has already entered its critical region), and the two run in parallel.

您应该全局分配锁对象,并且该锁对象应该在任一线程可能尝试进入其关键区域之前一直存在.

You should allocate the lock object globally, and it should persist from before the time either thread might attempt to enter its critical region.