如何终止等待@synchronized目标C的线程

时间:2021-07-20 20:57:31

I have some code like this:

我有一些像这样的代码:

doDatabaseFetch {
   ...
   @synchronized(self) {
      ...
   }
}

and many objects that call doDatabaseFetch as the user uses the view.

以及在用户使用视图时调用doDatabaseFetch的许多对象。

My problem is, I have an operation (navigate to the next view) that also requires a database fetch. My problem is that it hits the same synchronize block and waits it's turn! I would ideally like this operation to kill all the threads waiting or give this thread a higher priority so that it can execute immediately.

我的问题是,我有一个操作(导航到下一个视图),也需要数据库提取。我的问题是它击中了同一个同步块并等待轮到它了!理想情况下,我喜欢这个操作来杀死所有等待的线程,或者给这个线程一个更高的优先级,以便它可以立即执行。

Apple says that

Apple说

The recommended way to exit a thread is to let it exit its entry point routine normally. Although Cocoa, POSIX, and Multiprocessing Services offer routines for killing threads directly, the use of such routines is strongly discouraged.

退出线程的推荐方法是让它正常退出其入口点例程。尽管Cocoa,POSIX和Multiprocessing Services提供了直接杀死线程的例程,但强烈建议不要使用此类例程。

So I don't think I should kill the threads... but how can I let them exit normally if they're waiting on a synchronized block? Will I have to write my own semaphore to handle this behavior?

所以我不认为我应该杀死线程......但是如果他们在同步块上等待,我怎么能让它们正常退出?我是否必须编写自己的信号量来处理这种行为?

Thanks! Nick.

2 个解决方案

#1


The first question to ask here - do you need that big of a critical section so many threads are waiting to enter? What you are doing here is serializing parallel execution, i.e. making your program single-threaded again (but slower.) Reduce the lock scope as much as possible, think about reducing contention at the application level, use appropriate synchronization tools (wait/signal) - you'll find that you don't need to kill threads, pretty much ever. I know it's a very general advise, but it really helps to think that way.

这里要问的第一个问题 - 你是否需要大量的关键部分这么多线程都在等待进入?你在这里做的是序列化并行执行,即使你的程序再次单线程(但速度较慢。)尽可能减少锁定范围,考虑减少应用程序级别的争用,使用适当的同步工具(等待/信号) - 你会发现你几乎不需要杀死线程。我知道这是一个非常普遍的建议,但这样思考真的很有帮助。

#2


Typically you cannot terminate a thread that is waiting on a synchronized block, if you need that sort of behavior, you should be using a timed wait and signal paradigm so that threads are sound asleep waiting and can be interrupted. Plus if you use a timed wait and signal paradigm, each time the timed wait expires your threads have the opportunity to not go back to sleep but rather to exit or take some other path (ie. even if you don't choose to terminate them).

通常,您无法终止正在等待同步块的线程,如果您需要这种行为,您应该使用定时等待和信号范例,以便线程处于睡眠等待状态并且可以被中断。此外,如果您使用定时等待和信号范例,每次定时等待到期时您的线程都有机会不再回到睡眠状态而是退出或采取其他路径(即使您不选择终止它们) )。

Synchronized blocks are designed for uncontested locks, on an uncontested lock, the synchronization should be pretty close to a noop, but as soon as the lock becomes contested they have a very detrimental to application performance, moreso than even simply because they are serializing your parallel program.

同步块是针对无争议的锁而设计的,在无争议的锁上,同步应该非常接近noop,但是一旦锁变得有争议,它们对应用程序性能非常不利,甚至仅仅因为它们正在序列化你的并行程序。

I'm not an Objective C expert by any means, but I'm sure that there are some more advanced synchronization patterns such as barriers, conditions, atomics, etc.

我无论如何都不是Objective C专家,但我确信有一些更先进的同步模式,如障碍,条件,原子等。

#1


The first question to ask here - do you need that big of a critical section so many threads are waiting to enter? What you are doing here is serializing parallel execution, i.e. making your program single-threaded again (but slower.) Reduce the lock scope as much as possible, think about reducing contention at the application level, use appropriate synchronization tools (wait/signal) - you'll find that you don't need to kill threads, pretty much ever. I know it's a very general advise, but it really helps to think that way.

这里要问的第一个问题 - 你是否需要大量的关键部分这么多线程都在等待进入?你在这里做的是序列化并行执行,即使你的程序再次单线程(但速度较慢。)尽可能减少锁定范围,考虑减少应用程序级别的争用,使用适当的同步工具(等待/信号) - 你会发现你几乎不需要杀死线程。我知道这是一个非常普遍的建议,但这样思考真的很有帮助。

#2


Typically you cannot terminate a thread that is waiting on a synchronized block, if you need that sort of behavior, you should be using a timed wait and signal paradigm so that threads are sound asleep waiting and can be interrupted. Plus if you use a timed wait and signal paradigm, each time the timed wait expires your threads have the opportunity to not go back to sleep but rather to exit or take some other path (ie. even if you don't choose to terminate them).

通常,您无法终止正在等待同步块的线程,如果您需要这种行为,您应该使用定时等待和信号范例,以便线程处于睡眠等待状态并且可以被中断。此外,如果您使用定时等待和信号范例,每次定时等待到期时您的线程都有机会不再回到睡眠状态而是退出或采取其他路径(即使您不选择终止它们) )。

Synchronized blocks are designed for uncontested locks, on an uncontested lock, the synchronization should be pretty close to a noop, but as soon as the lock becomes contested they have a very detrimental to application performance, moreso than even simply because they are serializing your parallel program.

同步块是针对无争议的锁而设计的,在无争议的锁上,同步应该非常接近noop,但是一旦锁变得有争议,它们对应用程序性能非常不利,甚至仅仅因为它们正在序列化你的并行程序。

I'm not an Objective C expert by any means, but I'm sure that there are some more advanced synchronization patterns such as barriers, conditions, atomics, etc.

我无论如何都不是Objective C专家,但我确信有一些更先进的同步模式,如障碍,条件,原子等。