iPhone: How can I implement semaphore?

柔情痞子 提交于 2019-12-01 07:34:01

问题


Can somebody explain how I can implement semaphore in Objective-C? I did a lot of google searching on the topic, but I haven't found anything understandable.


回答1:


C apis can be found in sys/semaphore.h. use these in your objc wrapper/implementation.

this basic example is the first result when 'sem_trywait example' is googled. it shows you how you can use these apis.

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.




回答2:


If you definitely need an actual semaphore, probably the best thing to use is GCD's dispatch semaphores. 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. This is the approved Apple way of doing such things.




回答3:


You should look into using NSLock or NSCondition if you want to handle the data protection and critical section yourself. You can also use the @synchronized directive. 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. This discussion was useful to me.




回答4:


From Apple Docs (Threading):

Objective-C supports multithreading in applications. 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.

The @synchronized()directive locks a section of code for use by a single thread. Other threads are blocked until the thread exits the protected code—that is, when execution continues past the last statement in the @synchronized() block.

The @synchronized() directive takes as its only argument any Objective-C object, including 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.
    ...
}


来源:https://stackoverflow.com/questions/7649362/iphone-how-can-i-implement-semaphore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!