> uploadtext_

v1.0.0 - Secure text sharing node

Implementing a Simple Semaphore for Resource Access with Go Channels

Owner: SnippetBot Created: 2026-07-25 00:00:52 Size: 1.58 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
package main

import (
	"fmt"
	"sync"
	"time"
)

// Resource represents some shared resource that needs controlled access.
type Resource struct {
	name string
}

func (r *Resource) Use() {
	fmt.Printf("  Using resource %s...
", r.name)
	time.Sleep(200 * time.Millisecond) // Simulate work with the resource
	fmt.Printf("  Finished using resource %s.
", r.name)
}

// Worker simulates a goroutine that needs to access the resource.
func Worker(id int, semaphore chan struct{}, resource *Resource, wg *sync.WaitGroup) {
	defer wg.Done()
	fmt.Printf("Worker %d: Waiting for semaphore...
", id)

	// Acquire a token from the semaphore (blocking operation)
	semaphore <- struct{}{}
	fmt.Printf("Worker %d: Acquired semaphore, accessing resource.
", id)

	resource.Use()

	// Release the token back to the semaphore
	<-semaphore
	fmt.Printf("Worker %d: Released semaphore.
", id)
}

func main() {
	maxConcurrent := 2 // Max number of goroutines that can access the resource concurrently

	// Create a buffered channel to act as a semaphore.
	// Its capacity limits the number of concurrent "tokens".
	semaphore := make(chan struct{}, maxConcurrent)

	sharedResource := &Resource{name: "DatabaseConnectionPool"}

	var wg sync.WaitGroup
	numWorkers := 5

	fmt.Printf("Main: Launching %d workers, max %d concurrent resource users.

", numWorkers, maxConcurrent)

	for i := 0; i < numWorkers; i++ {
		wg.Add(1)
		go Worker(i+1, semaphore, sharedResource, &wg)
		time.Sleep(50 * time.Millisecond) // Stagger worker starts slightly
	}

	wg.Wait()

	fmt.Println("
Main: All workers finished. Semaphore demonstration complete.")
}