1
0
Fork 0
forked from fun/fun

Added Thread class to stdlib. (0.21.3)

This commit is contained in:
Johannes Findeisen 2025-10-04 12:46:53 +02:00
commit e2e2ac6a66
3 changed files with 105 additions and 1 deletions

39
lib/io/thread.fun Normal file
View file

@ -0,0 +1,39 @@
/*
* This file is part of the Fun programming language.
* https://hanez.org/project/fun/
*
* Copyright 2025 Johannes Findeisen
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-10-04
*/
/*
* Thread utilities built on thread_spawn() and thread_join().
*
* Methods:
* spawn(func, args) -> thread id number (args may be a single value or an array of values)
* join(id) -> returns the thread function's return value
*
* Convenience aliases:
* start(func, args) -> same as spawn()
* wait(id) -> same as join()
*/
class Thread()
// Spawn a new thread running function `func` with `args`.
// `args` can be a single argument or an array of arguments for the function.
fun spawn(this, func, args)
return thread_spawn(func, args)
// Join a thread by id and get its return value.
fun join(this, id)
return thread_join(id)
// Aliases for readability
fun start(this, func, args)
return thread_spawn(func, args)
fun wait(this, id)
return thread_join(id)