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


Groups > comp.programming.threads > #6920

First async program [Swift]

Newsgroups comp.programming.threads
From Branimir Maksimovic <branimir.maksimovic@icloud.com>
Subject First async program [Swift]
Message-ID <19n6J.5876$2m1.3618@fx26.iad> (permalink)
Organization usenet-news.net
Date 2021-10-03 19:04 +0000

Show all headers | View raw


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

Back to comp.programming.threads | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web