MPTask equivalent?
-
I am moving a rendering engine of sorts from Carbon to Cocoa. It is heavily
based on MPTasks and uses Carbon events to send messages back to the main
event handler (on the main thread) to handle things that are not thread
safe.
In Carbon, I would Post an event and call:
err = MPWaitOnQueue (pTaskInfo->mSignalQueueID, (void **)
&pTaskInfo->mQueueMessage, NULL, NULL, kDurationForever);
if (pTaskInfo->mQueueMessage == kMyTaskSignalAbort)
pTaskInfo->mAbort = true;
When the main thread would be done with the task, it would call:
MPNotifyQueue (pTaskInfo->mSignalQueueID, (void *) kMyTaskSignalContinue,
NULL, NULL);
Is there a good online reference to the Cocoa equivalent of this?
Any pointers as to how I should structure this in Cocoa?
How much of Cocoa is thread safe?
Thanks,
Trygve -
On Feb 20, 2008, at 8:03 PM, Trygve Inda wrote:
> I am moving a rendering engine of sorts from Carbon to Cocoa. It is
> heavily
> based on MPTasks and uses Carbon events to send messages back to the
> main
> event handler (on the main thread) to handle things that are not
> thread
> safe.
>
> In Carbon, I would Post an event and call:
>
> err = MPWaitOnQueue (pTaskInfo->mSignalQueueID, (void **)
> &pTaskInfo->mQueueMessage, NULL, NULL, kDurationForever);
> if (pTaskInfo->mQueueMessage == kMyTaskSignalAbort)
> pTaskInfo->mAbort = true;
>
> When the main thread would be done with the task, it would call:
>
> MPNotifyQueue (pTaskInfo->mSignalQueueID, (void *)
> kMyTaskSignalContinue,
> NULL, NULL);
>
> Is there a good online reference to the Cocoa equivalent of this?
>
> Any pointers as to how I should structure this in Cocoa?
You could probably use:
-[NSObject performSelectorOnMainThread:withObject:waitUntilDone:];
> How much of Cocoa is thread safe?
Very little, and the documentation on that topic is somewhat lacking.
j o a r -
--- Trygve Inda <cocoa...> wrote:
> I am moving a rendering engine of sorts from Carbon
> to Cocoa. It is heavily
> based on MPTasks and uses Carbon events to send
> messages back to the main
> event handler (on the main thread) to handle things
> that are not thread
> safe.
>
> In Carbon, I would Post an event and call:
>
> err = MPWaitOnQueue (pTaskInfo->mSignalQueueID,
> (void **)
> &pTaskInfo->mQueueMessage, NULL, NULL,
> kDurationForever);
> if (pTaskInfo->mQueueMessage == kMyTaskSignalAbort)
> pTaskInfo->mAbort = true;
>
> When the main thread would be done with the task, it
> would call:
>
> MPNotifyQueue (pTaskInfo->mSignalQueueID, (void *)
> kMyTaskSignalContinue,
> NULL, NULL);
>
>
> Is there a good online reference to the Cocoa
> equivalent of this?
If you can require 10.5, you might look at
NSOperation/NSOperationQueue. I haven't used the
Carbon Multiprocessing Services, but it sounds like
that should be roughly equivalent.
>
> How much of Cocoa is thread safe?
http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/Th
readSafetySummary/chapter_950_section_2.html
Cheers,
Chuck
____________________________________________________________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping -
>
> Is there a good online reference to the Cocoa equivalent of this?
>
> Any pointers as to how I should structure this in Cocoa?
>
> How much of Cocoa is thread safe?
Others have given some good specific suggestions for your problem.
For some more general info on multithreading facilities in Cocoa,
check out the Threading Programming Guide:
http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/in
dex.html -
>>
>> Is there a good online reference to the Cocoa equivalent of this?
>>
>> Any pointers as to how I should structure this in Cocoa?
>>
>> How much of Cocoa is thread safe?
>
> Others have given some good specific suggestions for your problem.
> For some more general info on multithreading facilities in Cocoa,
> check out the Threading Programming Guide:
>
> http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/in
dex
> .html
One thing that I can't seem to do in Cocoa is:
MPWaitOnQueue (pTaskInfo->mSignalQueueID, (void **)
&pTaskInfo->mQueueMessage, NULL, NULL, GetBlockDuration (pTaskInfo));
if (pTaskInfo->mQueueMessage == kMapTaskSignalAbort)
{
pTaskInfo->mAbort = true;
break;
}
That is, to pause a task for a fixed about of time... I have a task that I
want to take 5 minutes to do so that I use very little processor time. So I
frequently block for a variable amount of time.
Can I do this with Cocoa threads?
Is there a way to mix MPTasks in Cocoa? I am really eager to use Cocoa here
as many things are much easier than in Carbon (less code), but the
task/multithreading of Cocoa seems weak.
Trygve -
>>>http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/in
>>> Is there a good online reference to the Cocoa equivalent of this?
>>>
>>> Any pointers as to how I should structure this in Cocoa?
>>>
>>> How much of Cocoa is thread safe?
>>
>> Others have given some good specific suggestions for your problem.
>> For some more general info on multithreading facilities in Cocoa,
>> check out the Threading Programming Guide:
>>
>>
de>>
x
>> .html
>
> One thing that I can't seem to do in Cocoa is:
>
> MPWaitOnQueue (pTaskInfo->mSignalQueueID, (void **)
> &pTaskInfo->mQueueMessage, NULL, NULL, GetBlockDuration (pTaskInfo));
> if (pTaskInfo->mQueueMessage == kMapTaskSignalAbort)
> {
> pTaskInfo->mAbort = true;
> break;
> }
>
Just a follow up... The key here is that Carbon MPTasks allow a message to
be passed around for thread semaphore control... Actually Cocoa seems to not
allow any semaphore control at all.
Trygve -
On Feb 21, 2008, at 4:10 PM, Trygve Inda wrote:
> That is, to pause a task for a fixed about of time... I have a task
> that I
> want to take 5 minutes to do so that I use very little processor
> time. So I
> frequently block for a variable amount of time.
That sounds a bit strange to me. Could you perhaps use thread
priorities instead?
> Can I do this with Cocoa threads?
Perhaps you could use "-[NSThread sleepUntilDate:]"?
<http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes
/NSThread_Class/Reference/Reference.html#//apple_ref/occ/clm/NSThread/sleep
UntilDate:>
You probably also want to read up on Cocoa threads in general:
<http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/>
<http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes
/NSThread_Class/Reference/Reference.html>
On Feb 21, 2008, at 4:14 PM, Trygve Inda wrote:
> Just a follow up... The key here is that Carbon MPTasks allow a
> message to
> be passed around for thread semaphore control... Actually Cocoa
> seems to not
> allow any semaphore control at all.
Perhaps I don't understand what you're asking for, but in general I
don't think that's correct:
<http://developer.apple.com/documentation/Cocoa/Conceptual/Multithreading/Th
readSafety/chapter_5_section_7.html>
j o a r -
> Is there a way to mix MPTasks in Cocoa? I am really eager to use Cocoa here
> as many things are much easier than in Carbon (less code), but the
> task/multithreading of Cocoa seems weak.
Yes, you can create MPTasks from Cocoa, and use Cocoa from MPTasks. The key
is that there's a whole lot of Cocoa that's not thread-safe, but you can
certainly call performSelectorOnMainThread:withObject:waitUntilDone: from an
MPTask. Object allocation is thread safe, so you can create Objective-C
objects to pass through to that call, and you can create your own
Objective-C classes that are thread-safe.
You can also pass pointers to Objective-C objects through MPQueues. Just
watch out for the race condition of passing an autoreleased object via an
MPQueue--retain the object before putting it on the queue and release it (at
a CORRECT time) after getting it from the queue.
--
Scott Ribe
<scott_ribe...>
http://www.killerbytes.com/
(303) 722-0567 voice -
>> Is there a way to mix MPTasks in Cocoa? I am really eager to use Cocoa here
>> as many things are much easier than in Carbon (less code), but the
>> task/multithreading of Cocoa seems weak.
>
> Yes, you can create MPTasks from Cocoa, and use Cocoa from MPTasks. The key
> is that there's a whole lot of Cocoa that's not thread-safe, but you can
> certainly call performSelectorOnMainThread:withObject:waitUntilDone: from an
> MPTask. Object allocation is thread safe, so you can create Objective-C
> objects to pass through to that call, and you can create your own
> Objective-C classes that are thread-safe.
>
> You can also pass pointers to Objective-C objects through MPQueues. Just
> watch out for the race condition of passing an autoreleased object via an
> MPQueue--retain the object before putting it on the queue and release it (at
> a CORRECT time) after getting it from the queue.
I would like to use NSThreads as they seem simpler and Apple's latest docs
discourages MPTasks.
The call I need an equivalent to is MPWaitOnQueue... This blocks a thread
until a message is received OR it times out. Is there a way to get a similar
functionality with NSThread?
Basically I want to do a bit of processing, then wait for say 5 seconds, but
in the interim, I want to be able to send a abort signal to the thread so
that if I need to end the thread it can safely exit asap.
Thanks,
Trygve -
2008/3/10 Trygve Inda <cocoa...>:
> The call I need an equivalent to is MPWaitOnQueue... This blocks a thread
> until a message is received OR it times out. Is there a way to get a similar
> functionality with NSThread?
The most Cocoa-ish way to do this is probably to use run loops and
NSTimers, though you can also achieve it simply by using POSIX
select().
Hamish -
On Mar 10, 2008, at 4:05 PM, Trygve Inda wrote:
> I would like to use NSThreads as they seem simpler and Apple's
> latest docs
> discourages MPTasks.
>
> The call I need an equivalent to is MPWaitOnQueue... This blocks a
> thread
> until a message is received OR it times out. Is there a way to get
> a similar
> functionality with NSThread?
>
> Basically I want to do a bit of processing, then wait for say 5
> seconds, but
> in the interim, I want to be able to send a abort signal to the
> thread so
> that if I need to end the thread it can safely exit asap.
The documentation you're looking for is here:
http://developer.apple.com/documentation/Cocoa/Conceptual/
Multithreading/ThreadSafety/chapter_5_section_1.html
Using an NSConditionLock and its -lockWhenCondition:beforeDate:
method may be the most straightforward way to implement what you're
trying to achieve. (Note that NSConditionLock is discussed under
"Using Locks" in the above documentation, rather than under "Using
Conditions".)
-Ken



