diff --git a/CMakeLists.txt b/CMakeLists.txt index 03d8f14..d366211 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ 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_REQUIRED ON) diff --git a/examples/thread_class_example.fun b/examples/thread_class_example.fun new file mode 100755 index 0000000..90929df --- /dev/null +++ b/examples/thread_class_example.fun @@ -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 + +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 === +*/ diff --git a/lib/io/thread.fun b/lib/io/thread.fun new file mode 100644 index 0000000..2f76717 --- /dev/null +++ b/lib/io/thread.fun @@ -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)