On 9/22/04 11:23 AM, David Duncan didst favor us with:
> On Sep 22, 2004, at 11:11 AM, mark wrote:
>
>> So, it seems the general consensus is that I should start up just one
>> "disk
>> reading" thread vs multiple.
>>
>>>> I would assume that preemptive tasks would be the way to go, vs
>>>> threads.
>>
>>> MPTasks are implemented as preemptive threads, so do you mean vs.
>>> pthreads
>>> or vs. cooperative threads?
>>
>> In general, how would one choose between preemptive and cooperative
>> threads?
>
> In general one should favor preemptive threads simply because they make
> more efficient use of the CPU resources available (by definition
> cooperative threads can only share a fraction of one CPU since no two
> of them can be active at once). The only time you cannot use a
> preemptive thread is when you must use an API that is not thread safe,
> and even then there are ways to get around that if you are willing to
> do some work (this work involves getting that API to be run on the main
> thread and the results sent back to your preemptive thread).
All true, but just to offer the complete story, another advantage of
cooperative threads is that they don't have a lot of the issues which tend
to trip people up when using preemptive threads because you control when
they give up time. You don't have race conditions, every API is
cooperative-thread safe, you have fewer issues with resources allocated in
the thread and so on. The downsides are that they don't take advantage of
multiple CPUs (since only one is running at any one time) and the main
thread may not be quite as responsive, depending on how often the thread
yields time. The first isn't an issue if you're on a single-CPU Mac, and not
much of an issue if you don't have multiple threads which can working
concurrently. For example, if your threads were reading files, the
bottleneck would be the disk, not processing.
Given what you said you wanted to do, for example, cooperatives could
actually work better. You originally stated that you wanted to spawn a
thread for each of several directories to process the files in those
directories. If you do that with preemptive threads, you're performance will
likely suffer because of disk thrashing. OTOH, if those were cooperative
threads, then they could be coded to read an entire file before yielding
time, in which case the drive head wouldn't switch files in the middle of
reads. I'm not saying you *should* do this, but it's the kind of thing you
*can* do without a penalty using cooperative threads that would surely bite
you if you use preemptive threads.
Larry
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Mt-smp mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden