1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/sync/pool
2023-01-08 12:34:46 +02:00
..
pool_test.v all: replace generic <> with [] - part 2 (#16536) 2022-11-26 18:23:26 +02:00
pool.v sync.pool: fix pointer cast warning (#16902) 2023-01-08 12:34:46 +02:00
README.md all: replace generic <> with [] - part 2 (#16536) 2022-11-26 18:23:26 +02:00

The sync.pool module provides a convenient way to run identical tasks over an array of items in parallel, without worrying about thread synchronization, waitgroups, mutexes etc.., you just need to supply a callback function, that will be called once per each item in your input array.

After all the work is done in parallel by the worker threads in the pool, pool.work_on_items will return. You can then call pool.get_resultsResult to retrieve a list of all the results, that the worker callbacks returned for each input item. Example:

import sync.pool

pub struct SResult {
	s string
}

fn sprocess(mut pp pool.PoolProcessor, idx int, wid int) &SResult {
	item := pp.get_item[string](idx)
	println('idx: ${idx}, wid: ${wid}, item: ' + item)
	return &SResult{item.reverse()}
}

fn main() {
	mut pp := pool.new_pool_processor(callback: sprocess)
	pp.work_on_items(['1abc', '2abc', '3abc', '4abc', '5abc', '6abc', '7abc'])
	// optionally, you can iterate over the results too:
	for x in pp.get_results[SResult]() {
		println('result: ${x.s}')
	}
}

See https://github.com/vlang/v/blob/master/vlib/sync/pool/pool_test.v for a more detailed usage example.