• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Locks
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Locks


  • Subject: Re: Locks
  • From: koko <email@hidden>
  • Date: Tue, 06 Dec 2011 20:07:15 -0700

I did come across OSAtomicIncrementXX … thanks.

I cannot use NSLock as this is in a BSD Static lib and I am required to implement as close to windows as possible.

So my code looks like below and is isomorphic to the Windows implementation.



=========
LONG volatile Mylock = 0;

void SetLock()
{
	while(Mylock){Sleep(0);};		// Give up timeslice
#ifndef MAC
	InterlockedIncrement(&Mylock);
#else
	OSAtomicIncrement32(&Mylock);
#endif
}


void FreeLock()
{
#ifndef MAC
	OSInterlockedDecrement(&Mylock);
#else
	OSAtomicDecrement32(&Mylock);
#endif
}
===========





Thanks.

-koko



On Dec 6, 2011, at 5:39 PM, Conrad Shultz wrote:

> On 12/6/11 3: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 :
>
> Have you read the Threading Programming Guide?
>
> I'm not a Windows developer, so I had to look up the MSDN docs, and
> based on those it seems like you want the OSAtomicIncrementxxx (and
> matching OSAtomicDecrementxxx) as direct analogues.
>
> However, since it sounds like you are trying to use these functions to
> implement locking in a multithreaded application, perhaps you should
> also examine whether higher level constructs would prove sufficient.  A
> couple such mechanisms you can read about are NSLock and the
> @synchronized directive.  GCD has some features (dispatch semaphores and
> barrier blocks come to mind) that may also be helpful in certain
> circumstances.
>
> --
> Conrad Shultz
>
> Synthetiq Solutions
> www.synthetiqsolutions.com
>

_______________________________________________

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

  • Follow-Ups:
    • Re: Locks
      • From: Ken Thomases <email@hidden>
    • Re: Locks
      • From: "Stephen J. Butler" <email@hidden>
References: 
 >Locks (From: koko <email@hidden>)
 >Re: Locks (From: Conrad Shultz <email@hidden>)

  • Prev by Date: Re: Exceptions and autorelease pools
  • Next by Date: Re: Locks
  • Previous by thread: Re: Locks
  • Next by thread: Re: Locks
  • Index(es):
    • Date
    • Thread