Added Thread class to stdlib. (0.21.3)
This commit is contained in:
parent
cde0c45ea3
commit
e2e2ac6a66
3 changed files with 105 additions and 1 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
cmake_minimum_required(VERSION 3.16)
|
cmake_minimum_required(VERSION 3.16)
|
||||||
project(fun VERSION 0.21.2 LANGUAGES C)
|
project(fun VERSION 0.21.3 LANGUAGES C)
|
||||||
|
|
||||||
set(CMAKE_C_STANDARD 11)
|
set(CMAKE_C_STANDARD 11)
|
||||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
|
|
|
||||||
65
examples/thread_class_example.fun
Executable file
65
examples/thread_class_example.fun
Executable file
|
|
@ -0,0 +1,65 @@
|
||||||
|
#!/usr/bin/env fun
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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 class example using the stdlib Thread wrapper around thread_spawn/join.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <io/thread.fun>
|
||||||
|
|
||||||
|
print("=== Thread class example ===")
|
||||||
|
|
||||||
|
// worker that returns a square
|
||||||
|
fun square(n)
|
||||||
|
sleep(50)
|
||||||
|
return n * n
|
||||||
|
|
||||||
|
// Another worker accepting 3 args
|
||||||
|
fun add3(a, b, c)
|
||||||
|
return a + b + c
|
||||||
|
|
||||||
|
// Use the Thread class
|
||||||
|
thr = Thread()
|
||||||
|
|
||||||
|
ids = []
|
||||||
|
for x in [1, 2, 3, 4, 5]
|
||||||
|
id = thr.spawn(square, x)
|
||||||
|
push(ids, id)
|
||||||
|
print("spawned thread id=" + to_string(id) + " for x=" + to_string(x))
|
||||||
|
|
||||||
|
print("Joining threads...")
|
||||||
|
results = []
|
||||||
|
for id in ids
|
||||||
|
r = thr.join(id)
|
||||||
|
push(results, r)
|
||||||
|
|
||||||
|
print("Squares: " + to_string(results))
|
||||||
|
|
||||||
|
// Pass multiple args via array
|
||||||
|
id2 = thr.start(add3, [10, 20, 30])
|
||||||
|
print("join add3 -> " + to_string(thr.wait(id2)))
|
||||||
|
|
||||||
|
print("=== Thread class example done ===")
|
||||||
|
|
||||||
|
/* Expected output (ids may vary):
|
||||||
|
=== Thread class example ===
|
||||||
|
spawned thread id=1 for x=1
|
||||||
|
spawned thread id=2 for x=2
|
||||||
|
spawned thread id=3 for x=3
|
||||||
|
spawned thread id=4 for x=4
|
||||||
|
spawned thread id=5 for x=5
|
||||||
|
Joining threads...
|
||||||
|
Squares: [array n=5]
|
||||||
|
join add3 -> 60
|
||||||
|
=== Thread class example done ===
|
||||||
|
*/
|
||||||
39
lib/io/thread.fun
Normal file
39
lib/io/thread.fun
Normal 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)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue