RE: Re: How to use NSLock
RE: Re: How to use NSLock
- Subject: RE: Re: How to use NSLock
- From: "Peter Karlsson" <email@hidden>
- Date: Mon, 3 Jan 2005 13:00:14 +0100
Ok, I have made som improvements to my app but I got one problem as the
result.
I use this code:
----------
// send a sysex MIDI request here
// wait for all sysex data to be recieved
[myLock lockWhenCondition:TRANSFER_DONE];
// Save sysex data
// unlock and continue
[myLock unlockWithCondition:TRANSFER_DONE];
----------
I can now wait dor the sysex data in my second thread until all data is in
my memory buffer but:
If my device does not answer the request within 1 second, how can I
implement a retry function? Let's say I want to retry 4 times before I give
up. Suggestions how to solve this?
Peter
Ursprungligt meddelande
> On Sun, 02 Jan 2005 10:22:04 +0100, Peter Karlsson <email@hidden>
wrote:
> > Dear list!
> >
> > Can some please explain how I use NSLock to protect a variable from
being
> > used or destroyed by another thread?
> >
> > My app have 2 threads and a readProc. The normal thread that is always
> > there and a second thread that starts when I want to request a sysex
dump
> > from my synthesizer.
> >
> > The variable 'sysexcounter' is used in 2 places.
> >
> > 1 - 'sysexcounter' is incremented In my readProc that get the MIDI bytes
> >
> > 2 - In my second thread my app waits for 'sysexcounter' to get a
predefined
> > value that is the size of my sysex message before the code flow
continues.
> >
> > But it seems that something happens that makes 'sysexcounter' useless.
So I
> > want to protect it from being used by 2 places at the same time. I think
> > that is the right way to explain my problem.
>
> OK, I'm assuming you have something like the following psuedo-code:
>
> int sysexcounter;
>
> SomeCallbackFunction()
> {
> sysexcounter = ...;
> }
>
> SomeOtherFunction()
> {
> //Running in some other thread
> while(sysexcounter != predefined)
> ;
>
> //Do something
> ...;
> }
>
> If so, you need two things:
> 1) sysexcounter needs to be volatile, otherwise, the compiler can
> assume that it won't be changed by other threads, and the complier
> will be perfectly free to optimize the above while loop to something
> like:
>
> if(sysexcounter != predefined)
> {
> while(1)
> ;
> }
>
> 2) Some locking (as you suspected)
>
> Try something like:
> volatile int sysexcounter;
> NSLock *sysexLock;
>
> SomeInitializationFunction()
> {
> //Must be called before second thread is spawned
> //Maybe from some class' +initialize method, or from main()
>
> sysexLock = [[NSLock alloc] initialize];
> }
>
> SomeCallbackFunction()
> {
> [sysexLock lock];
> sysexcounter = ...;
> [sysexLock unlock];
> }
>
> SomeOtherFunction()
> {
> //Running in some other thread
> BOOL keepGoing = YES;
>
> while(keepGoing)
> {
> [sysexLock lock];
> keepGoing = (sysexcounter != predefined);
> [sysexLock unlock];
> }
>
> //Do something
> ...;
> }
>
> Essentially, the idea is that you *only* access sysexcounter between
> calls to [sysexLock lock] and [sysexLock unlock].
>
>
> --
> Clark S. Cox III
> email@hidden
> http://www.livejournal.com/users/clarkcox3/
> http://homepage.mac.com/clarkcox3/
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden