Waitgroups in Scilab

Our example demonstrates how to wait for multiple tasks to finish using Scilab’s parallel_run function. This is similar to the concept of wait groups in other languages.

function worker(id)
    mprintf("Worker %d starting\n", id);
    sleep(1000);  // Sleep for 1 second to simulate an expensive task
    mprintf("Worker %d done\n", id);
endfunction

function main()
    workers = list();
    for i = 1:5
        workers($+1) = list(worker, i);
    end
    
    results = parallel_run(workers);
    
    // Note: The results are not used in this example
endfunction

main();

In this example, we define a worker function that simulates a task by printing a start message, sleeping for a second, and then printing a completion message.

The main function creates a list of worker tasks, each with its own ID. We use Scilab’s parallel_run function to execute these tasks concurrently.

To run this program, save it as parallel_workers.sce and execute it using Scilab:

$ scilab -nw -f parallel_workers.sce
Worker 1 starting
Worker 2 starting
Worker 3 starting
Worker 4 starting
Worker 5 starting
Worker 1 done
Worker 2 done
Worker 3 done
Worker 4 done
Worker 5 done

The order of workers starting and finishing may vary between executions due to the nature of parallel execution.

It’s important to note that Scilab’s parallel execution model is different from some other languages. It doesn’t have explicit “wait groups” or similar constructs. Instead, parallel_run automatically waits for all tasks to complete before returning.

Also, Scilab’s parallel execution capabilities may be limited compared to some other languages. It doesn’t support the same level of fine-grained control over concurrent execution that you might find in languages with more advanced concurrency features.

For more advanced use cases or better performance, you might need to consider using external libraries or different programming languages that offer more robust parallel processing capabilities.