Example #1
0
func boundedBufferSemaphore() {
	const bufferSize int = 20
	n := 100
	const producerSpeed int = 1
	const consumerSpeed int = 1 // 10
	in := 0
	out := 0
	var buffer [bufferSize]int
	mutex := semaphore.New(1)
	empty := semaphore.New(bufferSize)
	full := semaphore.New(bufferSize)
	// full.AcquireMany(bufferSize) // Initialized to zero.
	full.DrainPermits()
	wg := new(sync.WaitGroup)

	wg.Add(1)
	go func() {
		for i := 0; i < n; i++ {
			empty.Acquire()
			mutex.Acquire()

			buffer[in] = i
			in = (in + 1) % bufferSize
			fmt.Printf("Produced %d\n", i)

			mutex.Release()
			full.Release()

			time.Sleep(time.Duration(producerSpeed) * time.Millisecond)
		}
		wg.Done()
	}()

	wg.Add(1)
	go func() {
		for i := 0; i < n; i++ {
			full.Acquire()
			mutex.Acquire()

			// Consume buffer[out]
			out = (out + 1) % bufferSize
			fmt.Printf("Consumed %d\n", i)

			mutex.Release()
			empty.Release()
			time.Sleep(time.Duration(consumerSpeed) * time.Millisecond)

		}
		wg.Done()
	}()

	wg.Wait()
}
Example #2
0
func NewMITIE(filepath string) *MITIE {
	ner := C.mitie_load_named_entity_extractor(C.CString(filepath))
	sem := semaphore.New(4)
	return &MITIE{
		ner: ner,
		sem: sem,
	}
}