Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3274158
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:00:29+00:00 2026-05-17T19:00:29+00:00

I need to imlement in cocoa, a design that relies on multiple threads. I

  • 0

I need to imlement in cocoa, a design that relies on multiple threads.

I started at the CoreFoundation level – I created a CFMessagePort and attached it to the CFRunLoop, but it was very inconvenient as (unlike on other platforms) it needs to have a (systemwide) unique name, and CFMessagePortSendRequest does not process callbacks back to the current thread while waiting. Its possible to create my own CFRunLoopSource object, but building my own thread safe queue seems like overkill.

I then switched from using POSIX threads to NSThreads, calling performSelector:onThread: to send messages to other threads. This is far easier to use than the CFMessagePort mechanism, but again, performSelector:onThread: does not allow the main thread to send messages back to the current thread – and there is no return value.

All I need is a simple – inprocess – mechanism (so I hopefully don’t need to invent schemes to create ‘unique’ names) that lets me send a message (and wait for a reply) from thread A to thread B, and, while waiting for the message, allow thread B to send a message (and wait for a reply) to/from thread A.

A simple: A calls B re-entrantly calls A situation that’s so usual on a single thread, but is deadlock hell when the messages are between threads.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:00:29+00:00Added an answer on May 17, 2026 at 7:00 pm

    use -performSelectorOnThread:withObject:waitUntilDone:. The object you pass would be something that has a property or other “slot” that you can put the return value in. e.g.

    SomeObject* retObject = [[SomeObject alloc] init];
    [anotherObject performSelectorOnThread: whateverThread withObject: retObject waitUntilDone: YES];
    id retValue = [retObject retValue];
    

    If you want to be really sophisticated about it, instead of passing an object of a class you define, use an NSInvocation object and simply invoke it on the other thread (make sure not to invoke the same NSInvocation on two threads simultaneously) e.g.

    [invocation performSelectorOnMainThread:@selector(invoke) withObject:NULL waitUntilDone:YES];
    

    Edit

    if you don’t want to wait for the processing on the other thread to complete and you want a return value, you cannot avoid the other thread calling back into your thread. You can still use an invocation e.g.

    [comObject setInvocation: myInvocation];
    [comObject setCallingThread: [NSThread currentThread]];
    [someObject performSelectorOnMainThread: @selector(runInvocation:) withObject: comObject waitUntilDone: NO];
    
    // in someObject's implementation
    
    -(void) runInvocation: (ComObject*) comObject
    {
        [[comObject invocation] invoke];
        [self perfomSelector: @selctor(invocationComplete:) 
                    onThread: [comObject callingThread] 
                  withObject: [comObject invocation]];
    }
    

    If you don’t like to create a new class to pass the thread and the invocation, use an NSDictionary instead e.g.

    comObject = [NSDictionary dictionaryWithObjectsAndKeys: invocation, "@invocation" [NSThread currentThread], @"thread", nil];
    

    Be careful about object ownership. The various performSelector… methods retain both the receiver and the object until they are done but with asynchronous calls there might be a small window in which they could disappear if you are not careful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Cocoa app that will need access to multiple file transfer protocols,
I have a Cocoa application that displays an application-modal alert using the NSAlert class.
in my cocoa application, I need a custom NSCell for an NSTableView. This NSCell
I'm just getting started with Cocoa Bindings and while I've read through much of
Hello I need to implement four views splitters like in Maya, 3ds max, Blender
I have an requirement to implement a custom keyboard for a Cocoa Touch data
I want to implement a view in an iPhone application that is essentially like
Heys, I am writing something in Xcode. I use Core Data for persistency and
I'm trying to get a part of my window to react to drag and
In Objective-C, NSString has a method called stringByExpandingTildeInPath This method will take a string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.