package main // A system in Go to limit the number of running threads // This behaves a lot like the Java FixedThreadPool Executor // the idea is to create a single "channel" where a channel is a way // of passing messages into threads. // Then start the number of threads you want to allow to run. // Put messages onto the thread for the total number of tasks you // want to do in the threads. So then what happens is the thread runs in // a loop where it tries to read from the channel. If success, it does work. // When work complete, it reads from channel again. If fail to read (which // happens when there are no more messages to read) then thread dies. // You could have the threads do different things by putting into // the channel an instruction about what to do. // // Note that channels are usually used to implement message passing // between threads (which is what this is doing, just unidirectionally and // quite boring.) import ( "fmt" "sync" ) const xthreads = 3 // Total number of threads to use, excluding the main() thread const times = 100000000 // the amount of counting to do const reps = 8 // the number of times to count. var counter int64 // a stupid function func doSomething(times int) { for th:=0; th