Re: malloc/free problems within a thread
Re: malloc/free problems within a thread
- Subject: Re: malloc/free problems within a thread
- From: Wagner Truppel <email@hidden>
- Date: Sat, 29 Jul 2006 19:37:05 -0700
On Jul 29, 2006, at 6:12 PM, Ken Tozier wrote:
I'm scanning a large folder hierarchy on a server that takes quite
awhile to execute (avg 12 minutes) and I want the machine to be
usable while this is going on in the background. I'm adding and
removing objects to a mutable array allocated in the init method
from the thread.
It seems to me that the correct way to approach this problem is to
spawn a *single* thread that scans the folder hierarchy in question,
not one every 5 seconds. So long as the scanning thread is the only
one that manipulates your mutable array (until the scan process is
done), there's no need to synchronize its access by means of locks.
When the scan is done, the thread then should communicate to the
application (more precisely, to the application's main thread) that
the array is now built and ready for consumption, and then proceed to
exit gracefully. Meanwhile, the application's main thread can go on
doing its own business.
If you want, you can have the scanning thread set the value of a
"progress variable" (a counter of some sort, a percentage, or
something along those lines) which is then periodically (think
timer!) read by the application's main thread and displayed in the
application's user interface, thereby providing the user with some
feedback on the scanning progress. Since that progress variable *is*
being accessed by more than one thread, its access does need to be
synchronized, which you accomplish by bracketing its setting (in the
scanning thread) and its reading (in the main thread's timer code)
with a lock/unlock pair.
This way, you have at most 2 threads running at any time while the
application is active. A point to keep in mind is who is responsible
for allocating and releasing the mutable array. The sensible thing to
do is to have the application main thread allocate it, pass it to the
scanning thread at the time the thread is created, and forget about
it until the scanning is done, at which point the main application
thread (which should again be the only thread running) can safely use
the array and dispose of it after being done with it.
Wagner
_______________________________________________
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