From e9681394398663c42a982e307eb539b8964d5dc5 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 25 Mar 2025 20:11:08 +0200 Subject: [PATCH] examples: add sync_pool.v for easier testing/diagnosing issues with the `sync.pool` implementation on different platforms --- examples/sync_pool.v | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 examples/sync_pool.v diff --git a/examples/sync_pool.v b/examples/sync_pool.v new file mode 100644 index 0000000000..425fd0a839 --- /dev/null +++ b/examples/sync_pool.v @@ -0,0 +1,44 @@ +// This example illustrates how to use `sync.pool`, +// and how the various settings for VJOBS, work items etc can interact. +@[has_globals] +module main + +import log +import time +import runtime +import sync.pool + +const args = arguments() +const nitems = args[1] or { '10' }.int() +const njobs = args[2] or { runtime.nr_jobs().str() }.int() +const delay = args[3] or { '1000' }.int() + +__global msgs = chan string{cap: 1000} + +fn worker_sleep(mut p pool.PoolProcessor, item_idx int, worker_id int) voidptr { + item := p.get_item[int](item_idx) + msgs <- '# worker_id: ${worker_id:3}, item_idx: ${item_idx + 1:03}, item: ${item:6}, started' + time.sleep(delay * time.millisecond) + msgs <- '# worker_id: ${worker_id:3}, item_idx: ${item_idx + 1:03}, item: ${item:6}, finished.' + return pool.no_result +} + +fn logger() { + for { + msg := <-msgs + log.info(msg) + if msg == '>>> done' { + break + } + } +} + +fn main() { + t := spawn logger() + msgs <- '>>> nitems: ${nitems:6} | njobs: ${njobs:6} | delay_ms: ${delay:6}' + items := []int{len: nitems, init: index * 1000} + mut fetcher_pool := pool.new_pool_processor(callback: worker_sleep) + fetcher_pool.work_on_items(items) + msgs <- '>>> done' + t.wait() +}