Skip to content

main task needs to run on the main OS thread #2058

Closed

Description

@brson

In some operating systems (mac, win) the main thread is special and can do special things (on mac you have to be on the main thread to do cocoa API calls). Somehow we probably need to arrange for the main task to run on the main thread, which is a pain.

Activity

  1. brson commented on Mar 27, 2012

    @brson
    ContributorAuthor

    Here's the current plan. The main task continues to be non-special, but we create another single-threaded scheduler called 'main_thread_sched' that has a single scheduler thread running on the OS main thread. Users can choose to spawn tasks into this scheduler.

    Requires giving schedulers a first class representation in core, reworking how scheduler lifetime works, and refactoring a bunch of runtime code.

  2. ghost assigned on Mar 28, 2012
  3. graydon commented on Mar 28, 2012

    @graydon
    Contributor

    What a charming asymmetry. Full steam ahead!

  4. jamorton commented on Mar 29, 2012

    @jamorton
    Contributor

    Is that bit of flexibility worth such a convoluted solution? What's wrong with the user limiting their calls to those few libraries (like cocoa) to the main task?

  5. brson commented on Mar 29, 2012

    @brson
    ContributorAuthor

    @Jonanin that's a point worth considering. My concern previously was that making the main task special could have some subtle, non-obvious side effects.

    Off hand this is what I can think of:

    • The main task will be pinned to a single thread so could ocassionally get scheduled unfairly if there are bad neighbors on the same thread (assuming we have work stealing).
    • I believe there will need to be some extra work done by the runtime on the main OS thread between scheduler loops, though I'm not entirely sure yet.
    • In the case of an application that is actually using the OS event loop, one of the scheduler threads on the default scheduler is going to have a bunch of external work contending for time, and not performing the same as the other threads. For example, the runtime might be embedded in another application, with the main scheduler being driven from 'outside' by the OS's event loop, and between each turn of the scheduler loop you have to stop to dispatch OS messages. This would affect not just the performance of the main task, but any task that randomly gets scheduled onto that thread.
  6. brson commented on Mar 29, 2012

    @brson
    ContributorAuthor

    Of course, to combat that last point we could have the main task in it's own single-threaded scheduler and configured to spawn new tasks into a second scheduler that is more fair.

  7. brson commented on Mar 29, 2012

    @brson
    ContributorAuthor

    That in itself (the main thread spawning into a different scheduler) is some subtle behavior though that I'm not sure the implications of.

  8. jamorton commented on Mar 29, 2012

    @jamorton
    Contributor

    Not that it counts, since I don't know the full implications or full architecture of the system, but I vote for putting the main task in its own thread =]

  9. fzzzy commented on Mar 29, 2012

    @fzzzy
    Contributor

    brson, the initial design sounds right. There are too many downsides to having the main task always on the main thread. Better to have each consumer of an os API have to request to be on the main thread if it needs to be.

  10. brson commented on Apr 21, 2012

    @brson
    ContributorAuthor

    Implemented as a special scheduler.

  11. added a commit that references this issue on Dec 30, 2024
  12. added a commit that references this issue on Jan 2, 2025
  13. added a commit that references this issue on Mar 16, 2026
  14. added 2 commits that reference this issue on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-runtimeArea: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflows

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions