iphone - Should an NSLock instance be "global"? -


Should I use a single NSLock for example in the application representative, for all classes ? Or is it fair that start each of your classes according to your NSLock frequency?

Does locking work in another case, if I, for example, have access to a managed object presentation which has spread to two view controllers?

If multiple objects only access your object to read your content, Not at all if at least one object accesses your object to write / update your content, then it does not matter that other objects use to read or write / update your object: this In case you lock The Shykta.

Now, to keep your object safe (in a significant part of the code where many objects can access it), you should use the same lock instance, after which all of the objects Possible objects to protect you from whatever you want.

If your application is required to protect such an object, which can be reached simultaneously with the majority of classes, then it is okay to have a lock example. If you want a better performance (especially if the number of access is more along with your object), then you may have many locks, each lock will be responsible for access / rejection of your object's specific attribute / field. In this way, many objects can change the different attributes / fields at once to your object. You are basically increasing the number of concurrent actions on your object. However, each lock should still be shared among other objects, which are protecting the object.

A lock instance for each controller just does not work; This will not secure your object from concurrent access to other objects in different threads. NSLock has been implemented using POSIX pthread mutexes, so it should be used in the same way. This is also clearly stated in NSLock documentation:

Warning: NSLok uses posix threads to implement class locking behavior. When sending an unlock message to an NSLock object, you must ensure that the message is sent from a single thread that sends the initial lock message. Unlocking lock with a different thread can lead to undefined behavior.

Therefore, important sections to preserve the words, it is a thread that has secured the lock, which is responsible for issuing it, also keep in mind that the locking mechanism only operates fast , I.e. you should get a lock for a short time before releasing it. If you have to wait for an unexpected time, then you need a different synchronization mechanism, i.e. a condition variable which is available through the NScd class.

Hope it helps.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -