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 6244635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:21:37+00:00 2026-05-24T12:21:37+00:00

I know, that I am not the first one to try to use Cocoa

  • 0

I know, that I am not the first one to try to use Cocoa on OSX together with an existing c/c++ main loop, but I am not really liking the solutions I came across so far so I came up with a different idea I’d like to discuss. The most common way I found (in glut, glfw, SDL and also QT I think) is to use polling to replace NSApplications run method and process the events yourself with this:

nextEventMatchingMask:untilDate:inMode:dequeue:

This has the big disadvantage that the cpu is never really idle since you have to poll the whole time to check if there are any new events, furthermore its not the only thing going on inside NSApplications run function, so it might break some details if you use this replacement.

So what I’d like to do is to keep the cocoa runLoop intact. Imagine you’d have your own timer methods implemented in c++ which would usually be managed and fired inside your main loop (this is just a small part as an example). My idea would be to move all my looping portions to a secondary thread (since NSApplication run needs to be called from the main thread as far as I know), and then post custom Events to my derived version of NSApplication that handles them appropriately inside its sendEvent: method. For instance if my timers measured in my c++ loop fire, I’d post a custom event to NSApplication that in turn runs loopFunc() function of my application (residing in the mainthread as well) which appropriately sends the events down my c++ event chain.
So first of all, do you think this would be a good solution?
If yes, how would you implement that in cocoa, I only found this method inside the NSEvent Reference to post custom NSApplicationDefined events:

otherEventWithType:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:

and then use something like:

[NSApp postEvent:atStart:]

to notify NSApplication.

I’d rather post an event without any information about the window (in otherEventWithType), can I simply ignore that part?

Then I’d imagine to overwrite NSApplications sendEvent function similar to this:

    - (void)sendEvent:(NSEvent *)event
{
    //this is my custom event that simply tells NSApplication 
    //that my app needs an update
    if( [event type] == NSApplicationDefined)
    {
        myCppAppPtr->loopFunc(); //only iterates once
    }
        //transform cocoa events into my own input events
    else if( [event type] == NSLeftMouseDown)
    {
             ...
             myCppAppPtr->loopFunc(); //also run the loopFunc to propagate input events
    }
        ...

    //dont break the cocoa event chain
    [super sendEvent:event];

}

sorry for the long post, but this has been bothering me quite a bit since I am really not happy with what I found about this subject so far. Is this how I’d post and check for a custom event inside NSApplication, and do you think this is a valid approach to integrate cocoa into an existing runloop without polling?

  • 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-24T12:21:38+00:00Added an answer on May 24, 2026 at 12:21 pm

    okay, after all this took me way more time than I expected, and I’d like to outline the things I tried and tell you what experiences I had with them. This will hopefully save people trying to integrate Cocoa into an existing mainloop alot of time in the future. The first function I found when searching for the discussed matter was the function

    nextEventMatchingMask:untilDate:inMode:dequeue:
    

    but as I said in the question, my main problem with this was that I would have to constantly poll for new events which would waste quite some CPU Time.
    So I tried the following two methods to simply let my mainloops update function get called from the NSApplications mainloop:

    1. Post a custom Event to NSApplication, overwrite NSApplications
      sendEvent: function and simply call my mainloops update function
      from there. Similar to this:

      NSEvent* event = [NSEvent otherEventWithType: NSApplicationDefined
                                                           location: NSMakePoint(0,0)
                                                     modifierFlags: 0
                                                         timestamp: 0.0
                                                      windowNumber: 0
                                                           context: nil
                                                           subtype: 0
                                                             data1: 0
                                                             data2: 0];
                      [NSApp postEvent: event atStart: YES];
      
      
      //the send event function of my overwritten NSApplication
         - (void)sendEvent:(NSEvent *)event
      {
          //this is my custom event that simply tells NSApplication 
          //that my app needs an update
          if( [event type] == NSApplicationDefined)
          {
              myCppAppPtr->loopFunc(); //only iterates once
          }
      }
      

      This was only a good idea in theory because if my app updated very
      quickly (for instance due to a timer firing quickly), the whole
      cocoa event queue became totoally unresponsive because I added so
      many custom events. So don’t use this…

    2. Use performSelectorOnMainThread with a cocoaFunction that in
      turn calls my update function

      [theAppNotifier
      performSelectorOnMainThread:@selector(runMyMainLoop) withObject:nil
      waitUntilDone:NO ];
      

      This was alot better, the app and cocoa EventLoop was very
      responsive. If you are only trying to achieve something simple I’d
      recommend going down this route since it is the easiest of the ones
      proposed here. Anyways I had very little control about the order of
      things happening with this approach (this is crucial if you have a
      multithreaded app), i.e when my timers fired and would do a rather
      long job, often times they would reschedule before any new
      mouse/keyboard input could be added to my eventQueue and thus would
      make the whole input sluggish. Turnin on Vertical Sync on a window
      that was drawn by a repeating timer was enough to let this happen.

    3. After all I had to come back to nextEventMatchingMask:untilDate:inMode:dequeue: and after some trial and error I actually found a way to make it work without constant polling. The structure of my loop is similar to this:

      void MyApp::loopFunc()
      {
          pollEvents();
          processEventQueue();
          updateWindows();
          idle();
      }
      

      where pollEvents and idle are the important functions, basically I use something similar to this.

      void MyApp::pollEvents()
      {
          NSEvent * event;
      
          do
          {
              event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES];
      
      
                  //Convert the cocoa events to something useful here and add them to your own event queue
      
              [NSApp sendEvent: event];
          }
          while(event != nil);
      }
      

      To implement the blocking inside the idle() function I did this (not sure if this is good, but it seems to work great!) :

      void MyApp::idle()
      {
          m_bIsIdle = true;
          NSEvent * event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode dequeue:NO];
          m_bIsIdle = false;
      }
      

      this causes cocoa to wait till there is an event, if that happenes idle simply exits and the loopfunc starts again. To wake up the idle function if i.e. one of my timers (i dont use cocoa timers) fires, i once again use a custom event:

      void MyApp::wakeUp()
      {
          m_bIsIdle = false;
      
          //this makes sure we wake up cocoas run loop
          NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
          NSEvent* event = [NSEvent otherEventWithType: NSApplicationDefined
                                              location: NSMakePoint(0,0)
                                         modifierFlags: 0
                                             timestamp: 0.0
                                          windowNumber: 0
                                               context: nil
                                               subtype: 0
                                                 data1: 0
                                                 data2: 0];
          [NSApp postEvent: event atStart: YES];
          [pool release];
      }
      

      Since I clear the whole cocoa event queue right afterwards i don’t have the same problems as described in section 1.
      However, there are some drawbacks with this approach aswell because I think it does not do everything that [NSApplication run] is doing internally, i.e. application delegate things like this:

      - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)theApplication
      {
            return YES;
      }
      

      don’t seem to work, anyways I can live with that since you can easily check yourself if the last window just closed.

    I know this answer is pretty lengthy, but so was my journey to get there. I hope this helps someone and prevents people from doing the mistakes I did.

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

Sidebar

Related Questions

I know that XSLT does not work in procedural terms, but unfortunately I have
I know that the #warning directive is not standard C /C++, but several compilers
I know that cursors are frowned upon and I try to avoid their use
I know that that is not a question... erm anyway HERE is the question.
I know that Serialization (Serializable) is not available in the Micro Edition of Java.
I know that primary keys based on Guids do not have the best performance
I know that the f# list is not the same at the c# List.
I know that there are many free and not so free compression libraries out
I know that a SQL Server full text index can not index more than
Does anyone know of an IDE for F# development that does not involve me

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.