Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #6920 > unrolled thread

First async program [Swift]

Started byBranimir Maksimovic <branimir.maksimovic@icloud.com>
First post2021-10-03 19:04 +0000
Last post2021-10-03 19:04 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.programming.threads


Contents

  First async program [Swift] Branimir Maksimovic <branimir.maksimovic@icloud.com> - 2021-10-03 19:04 +0000

#6920 — First async program [Swift]

FromBranimir Maksimovic <branimir.maksimovic@icloud.com>
Date2021-10-03 19:04 +0000
SubjectFirst async program [Swift]
Message-ID<19n6J.5876$2m1.3618@fx26.iad>
import Foundation

@main
struct Main{
    static var i:Int = 0
    static func f()->Int{
        i += 1
        let j = i
        print("async ",j)
        return j
    }
    actor A{
        var future:[Int] = Array(repeating: 0, count: 10)
        func set(_ index:Int,_ val:Int){
            future[index] = val
        }
        func get(_ index:Int)->Int{
            return future[index]
        }
        func run()async{
            await withTaskGroup(of: Void.self) { group in
                for i in 0..<10 {
                    group.addTask{
                        print("starting")
                        await self.set(i, f())
                    }
                }
            }
        }
        func collect(){
            for i in 0..<10 {
                print("result ",get(i))
            }
        }
    }
    static func main()async{
        print("entering main")
        let a = A()
        await a.run()
        await a.collect()
    }
}


-- 

7-77-777
Evil Sinner!
to weak you should be meek, and you should brainfuck stronger
https://github.com/rofl0r/chaos-pp

[toc] | [standalone]


Back to top | Article view | comp.programming.threads


csiph-web