Re: Locks
Re: Locks
- Subject: Re: Locks
- From: Ken Thomases <email@hidden>
- Date: Tue, 06 Dec 2011 18:45:11 -0600
On Dec 6, 2011, at 5:28 PM, koko wrote:
> In windows we have:
>
> LONG volatile Mylock = 0;
> InterlockedIncrement(&Mylock);
> InterlockedDecrement(&Mylock);
>
>
> What should these be replaced with for OSX as in :
>
> #ifndef MAC
> LONG volatile Mylock = 0;
> #else
> // Mac
> #endif
>
>
> void SetLock()
> {
> // EnterCriticalSection(&m_cs);
> #ifndef MAC
> while(Mylock){Sleep(0);}; // Give up timeslice
> InterlockedIncrement(&Mylock);
> #else
> // Mac
> #endif
> }
>
>
> void FreeLock()
> {
> // LeaveCriticalSection(&m_cs);
> #ifndef MAC
> InterlockedDecrement(&Mylock);
> #else
> // Mac
> #endif
> }
Well, the use of atomic operations to implement a lock is pretty poor code. You should use a real locking primitive to achieve that. In Cocoa, look at NSLock and/or NSRecursiveLock. If you can't rely on Cocoa, use pthread's mutexes.
Read this <https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html> to learn more about synchronization primitives.
If you just want atomic increment and decrement, that page has a link to the man page for the relevant functions. But don't do a blind reimplementation of the poor code you're porting. Use the proper synchronization primitives.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
References: | |
| >Locks (From: koko <email@hidden>) |