[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Threads, DO and Runloop unexpected behaviour
From: |
Wim Oudshoorn |
Subject: |
Threads, DO and Runloop unexpected behaviour |
Date: |
Tue, 01 Nov 2005 15:54:13 +0100 |
User-agent: |
Gnus/5.1002 (Gnus v5.10.2) Emacs/22.0.50 (darwin) |
I am using DO to communicate between threads.
In the following simple example we have 3 threads.
- Worker thread, handling requests from thread A and B
- thread A performing -doInternal on the Worker thread
- thread B perfomring -doExternal on the Worker thread
Now the doExternal thread will wait until something happens
and in order to avoid blocking all other work, it will
explicitly run the runloop. So during execution of
-doExternal it should be possible that thread A gets its
requests for -doInternal processes. However, it sometimes does NOT!
Now in relevant code snippets:
- (void) doInternal
{
NSLog (@"TT -- Internal");
[NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.05]];
}
- (void) doExternal
{
int i;
NSLog (@"TT -- External");
for (i = 0; i < 20; ++i)
{
[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:
[NSDate dateWithTimeIntervalSinceNow:
0.005]];
}
}
The two threads A and B will continuously call -doInternal and -doExternal.
So I expect in the output lots of:
TT -- Internal
with sometimes an
TT -- External
and it should be impossible to have two lines:
TT -- External
next to each other (doExternal takes 0.1 sec and doInternal 0.05
so every doExternal timeframe should see at least one doInternal.)
However if I run the attached program I sometimes see
two TT -- Externals next too each other and I do not understand this.
I am probably using the NSRunloop wrong, but any insight is appreciated.
Oh and source of a test program is attached.
Wim Oudshoorn.
thread-test.tar.gz
Description: Test program
- Threads, DO and Runloop unexpected behaviour,
Wim Oudshoorn <=