简体   繁体   English

iPhone:我如何实现信号量?

[英]iPhone: How can I implement semaphore?

Can somebody explain how I can implement semaphore in Objective-C? 有人可以解释我如何在Objective-C中实现信号量? I did a lot of google searching on the topic, but I haven't found anything understandable. 我做了很多关于这个主题的谷歌搜索,但我没有发现任何可以理解的东西。

If you definitely need an actual semaphore, probably the best thing to use is GCD's dispatch semaphores . 如果你肯定需要一个实际的信号量,可能最好用的是GCD的调度信号量 I'd put an explanation in, but the code in the link is pretty simple. 我会给出一个解释,但链接中的代码非常简单。 You should be able to follow it. 你应该能够遵循它。

Basically, you create the semaphore with a parameter that indicates the number of concurrent instances of your resource you can have. 基本上,您使用一个参数创建信号量,该参数指示您可以拥有的资源的并发实例数。 Then threads that want to use the resource wait on the semaphore until one becomes available and signal it when they are done. 然后,想要使用资源的线程在信号量上等待,直到一个变为可用,并在完成后发出信号。

Consider using a dispatch queue or an NSOperationQueue instead with a concurrent limit of the number of instances of your resource. 考虑使用调度队列NSOperationQueue,而不是资源实例数的并发限制。 This is the approved Apple way of doing such things. 这是批准的Apple做这种事情的方式。

C apis can be found in sys/semaphore.h . 可以在sys/semaphore.h找到C apis。 use these in your objc wrapper/implementation. 在你的objc包装器/实现中使用它们。

this basic example is the first result when 'sem_trywait example' is googled. 这个基本的例子是google搜索'sem_trywait例'时的第一个结果。 it shows you how you can use these apis. 它向您展示了如何使用这些api。

then a minimal interface would take this form: 然后一个最小的接口将采取这种形式:

@interface MONSemaphore : NSObject
{
    sem_t semaphore;
}

- (int)close;
- (int)destroy;

/* .. and the rest of the interface you wrap and make public here .. */

@end

but it's likely that you will also want the object to abstract the init and destruct routines from the client. 但是您可能还希望该对象从客户端抽象init和破坏例程。

From Apple Docs (Threading) : 来自Apple Docs(线程)

Objective-C supports multithreading in applications. Objective-C支持应用程序中的多线程。 Therefore, two threads can try to modify the same object at the same time, a situation that can cause serious problems in a program. 因此,两个线程可以尝试同时修改同一个对象,这种情况可能会导致程序出现严重问题。 To protect sections of code from being executed by more than one thread at a time, Objective-C provides the @synchronized() directive. 为了保护代码段不被一次多个线程执行,Objective-C提供了@synchronized()指令。

The @synchronized()directive locks a section of code for use by a single thread. @synchronized()指令锁定一段代码以供单个线程使用。 Other threads are blocked until the thread exits the protected code—that is, when execution continues past the last statement in the @synchronized() block. 其他线程被阻塞,直到线程退出受保护的代码 - 也就是说,当执行继续超过@synchronized()块中的最后一个语句时。

The @synchronized() directive takes as its only argument any Objective-C object, including self. @synchronized()指令将任何Objective-C对象作为唯一参数,包括self。 This object is known as a mutual exclusion semaphore or mutex. 此对象称为互斥信号量或互斥量。 It allows a thread to lock a section of code to prevent its use by other threads. 它允许线程锁定一段代码以防止其他线程使用它。 You should use separate semaphores to protect different critical sections of a program. 您应该使用单独的信号量来保护程序的不同关键部分。 It's safest to create all the mutual exclusion objects before the application becomes multithreaded, to avoid race conditions. 在应用程序变为多线程之前创建所有互斥对象是最安全的,以避免竞争条件。

Example: 例:

Account *account = [Account accountFromString:[accountField stringValue]];

// Get the semaphore.
id accountSemaphore = [Account semaphore];

@synchronized(accountSemaphore) {
    // Critical code.
    ...
}

You should look into using NSLock or NSCondition if you want to handle the data protection and critical section yourself. 如果您想自己处理数据保护和关键部分,则应该考虑使用NSLockNSCondition You can also use the @synchronized directive . 您还可以使用@synchronized指令 You can also just use the usual POSIX thread API if you fancy it although it's not recommended as Cocoa gives you plenty of higher level stuff which is simpler and nicer. 如果您喜欢它,您也可以使用通常的POSIX线程API,尽管不建议使用它,因为Cocoa为您提供了更多更高级别的东西,它更简单,更好。 This discussion was useful to me. 这个讨论对我很有用。

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

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