How to implement Coroutines on iOS

I am porting a C++ project to iOS for use on iPhone and iPad. This project uses the Boost.Coroutine library extensively. Boost.Coroutine does not have an implementation for the iPhone's ARMv6/ARMv7 instruction set.

  • Are there other coroutine libraries that can run on iOS?

  • If not, is it possible to write coroutine behavior on ARM? I can see a few potential ways to do this:

  • Write assembly instructions directly to perform the stack manipulation. I am not very well versed in assembly, and I'm worried that the ARM architecture may not include the instructions necessary to copy & paste the stack, or to manually move the stack pointer.
  • Write coroutines using something similar to pthreads, or Win32 fibers. I'm not sure if there's something like this that could be used on iOS.
  • Implement coroutines, perhaps even Boost.Coroutine itself, on top of threads. This seems the most likely to work, but would definitely have performance drawbacks.
  • Note: Unity supports coroutines in C# on iOS; I'm not sure if this is limited to a smaller subset of typical coroutine behavior. If not, does this mean Unity has a solution?


    You almost certainly don't want to write assembly instructions to perform the stack manipulation. iOS is already on its third version of the ARM instruction set, having transitioned from ARMv6 through ARMv7 to ARMv7s. As of the iPhone 5, Apple has further added the artificial barrier that you may not submit an application with an ARMv6 fork that also supports the full iPhone 5 screen. I'm sure Apple's motivation is to ensure it can transition to processors without an ARMv6 compatibility mode at some point in the future but for us developers it obviously means not becoming too dependent on the specific instruction set.

    That leaves threads. iOS has a full set of well-developed threading mechanisms and pthread is there to expose the relevant subset. Grand Central Dispatch tends to be the normal solution used now to ensure that different tasks can occur simultaneously so swallows most of the Internet documentation but the lower-level solutions remain.

    Obvious trivial example, using an NSConditionLock:

    - (void)methodOnThread1
    {
        while(1)
        {
            [conditionLock lockWhenCondition:kMoreInputAvailable];
    
            // process whatever is currently in the common access pool
    
            [conditionLock unlockWithCondition:kWaitingForInput];
        }
    }
    
    - (void)methodOnThread2
    {
        while(1)
        {
             // do whatever is necessary to produce more input,
             // creating it locally and taking as long as it takes
    
    
            [conditionLock lockWhenCondition:kWaitingForInput];
    
            // push input to common access pool, by whatever means
    
            [conditionLock unlockWithCondition:kMoreInputAvailable];
        }
    }
    

    boost.coroutine(来自Oliver Kowalke;上个月来自boost社区)使用boost.context,它支持ARMv6(ARM Cortext Ax)

    链接地址: http://www.djcxy.com/p/53188.html

    上一篇: C ++绿色线程的堆栈分配

    下一篇: 如何在iOS上实现协同程序