Re: Thread safety of NSPipe
Re: Thread safety of NSPipe
- Subject: Re: Thread safety of NSPipe
- From: Glen Low <email@hidden>
- Date: Fri, 12 Mar 2004 14:11:21 +0800
On Feb 18, 2004, at 8:12 AM, Glen Low wrote:
Consider the following scenario:
1. Main thread creates an NSPipe.
2. Main thread creates a writing NSFileHandle.
3. Main thread creates a reading NSFileHandle and detaches a worker
thread, passing it the NSFileHandle.
4. Main thread writes to the writing NSFileHandle.
5. Worker thread reads from the reading NSFileHandle.
Am I going to get corrupted data? In all cases, operations are
performed on different threads on different objects. (Deallocating the
NSPipe seems problematic, but if it is a static variable, it doesn't
need to get deallocated.)
I should think that this would work, since this is basically what
happens when you use -[NSFileHandle readInBackgroundAndNotify], isn't
it? A worker thread gets spawned which reads from the NSFileHandle.
The issue here is a subtle one, and because of the inherent subtlety of
multithreaded work I'm inclined to ask for an official opinion rather
than jump in and do it... otherwise some user is going to be running
133 processes, opened 37 documents, clicking "just the right way" and
bam! data corruption --
One possible source of subtlety is the use of static data. Even if I
have two separate instances of NSFileHandle which each thread accesses,
if they "happen" to have some kind of shared or static data that isn't
properly mutexed, there will be data loss or corruption. Of course,
just talking about the underlying BSD constructs, the file handles do
share the pipe, but this use is documented to be threadsafe. If the
NSFileHandle only knows or accesses the pipe through its BSD file
handle, which I imagine it does, then everything should be hunky-dory.
Based on that analysis and some assurances from the gurus in this
group, I've "gone ahead" and implemented file change notification in
Graphviz and so far no-one's complained. (They probably aren't running
exactly 133 processes and have 37 documents open and have not clicked
on a certain particular spot... :-) ).
Cheers, Glen Low
---
pixelglow software | simply brilliant stuff
www.pixelglow.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.