; ; @ANO_SCRIPT_NAME thread_synchronizing ; @ANO_SCRIPT_VERSION 0.0.1 ; @ANO_SCRIPT_DESCRIPTION Simple thread synchronizing example ; ; @ANO_FLAGS_USE_PROTOS [ ] ; @ANO_FLAGS_VAR_NAME_SUBS [ ] ; @ANO_FLAGS_VAR_WARN_UNUSED [ ] ; ; Copyright (c) 2016-2026, Jani Salonen ; All rights reserved. ; ; Basic idea with this example is to create several reader threads and two ; writer threads. When writer access synchronized block, it will block all ; other threads until it exits from that block. All reader threads can access ; synchronized block simultaneously as long as there is no writer. ; main [exit: 0] { ; Create writer thread. ; thread_spawn (\ thread_name: DEFAULT_THREAD, \ thread_function: "my_writer") ; Create reader threads. ; for(mov _i (0); _i < 10; inc _i) { ; Use loop counter as thread name. ; mov _j (_i) ntos _j thread_spawn (\ thread_name: _j, \ thread_function: "my_reader") } ; Create another writer thread. ; thread_spawn (\ thread_name: DEFAULT_THREAD, \ thread_function: "my_writer") ; Wait for all threads to complete. ; thread_wait_all print "Threads done.\n" } thread my_reader { print "Reader thread is starting...\n" ; Get seconds to sleep. ; rand _r (4) inc _r synchronized [role: reader, oid: 0] { print "This is synchronized block, reader is sleeping here" \ " for " . _r . " seconds.\n" sleep (_r, 0) } print "...reader thread ends.\n" } thread my_writer { print "Writer thread is starting...\n" ; Get seconds to sleep. ; rand _r (9) inc _r synchronized [role: writer, oid: 0] { print "This is synchronized block, writer is blocking other" \ " threads and sleeping\nhere for " . _r . " seconds.\n" sleep (_r, 0) } print "...writer thread ends.\n" }