PB Coroutines Ver. 2 [Crossplatform]
Posted: Thu Apr 17, 2025 3:56 pm
I did a rewrite of my previous coroutine system (resumable functions):
https://www.purebasic.fr/english/viewto ... es#p635874
Now is cleaner and supports MacOS but only tested on Monterrey on a VM, it may not work on newer systems, testing will be appreciated.
It uses fibers on windows, libaco on linux and ucontext on macos.
Project at github:
https://github.com/omegakode/PBCoroutines
I changed a lot of things, if you need the old version make a backup first.
This is the example code:
https://www.purebasic.fr/english/viewto ... es#p635874
Now is cleaner and supports MacOS but only tested on Monterrey on a VM, it may not work on newer systems, testing will be appreciated.
It uses fibers on windows, libaco on linux and ucontext on macos.
Project at github:
https://github.com/omegakode/PBCoroutines
I changed a lot of things, if you need the old version make a backup first.
This is the example code:
Code: Select all
EnableExplicit
XIncludeFile "PBCoroutines.pb"
Procedure.l my_task1(co.i)
Debug "Task1: Step 1 "
Debug co_get_arg(co)
co_yield(co)
Debug "Task1: Step 2 End"
EndProcedure
Procedure.l my_task2(co.i)
Debug "Task2: Step 1 "
co_yield(co)
Debug "Task2: Step 2 End"
EndProcedure
Procedure main()
Protected.i co, co2
co = co_create(@my_task1(), 10)
co2 = co_create(@my_task2(), 20)
Debug "Main: Resume Task1"
co_resume(co)
Debug "Main: Back from Task1"
Debug "Main: Resume Task2"
co_resume(co2)
Debug "Main: Back from Task2"
Debug "Main: Resume Task1"
co_resume(co)
Debug "Main: Back from Task1"
Debug "Main: Resume Task2"
co_resume(co2)
co_destroy(co)
co_destroy(co2)
EndProcedure
main()