Golang Semaphore: Understanding and Implementing

Author

Reads 1K

Colorful kites soar over Semaphore Beach festival in South Australia, showcasing art and summer fun.
Credit: pexels.com, Colorful kites soar over Semaphore Beach festival in South Australia, showcasing art and summer fun.

A semaphore in Golang is a variable that controls the access to a shared resource by multiple threads. It's a simple but powerful tool for managing concurrency.

In Golang, a semaphore is typically implemented using a channel or a mutex. We'll explore both approaches in this article. A semaphore can be thought of as a gatekeeper that allows a limited number of threads to access a resource at a time.

A semaphore with a value of 5, for example, means that only 5 threads can access a shared resource simultaneously. This can be useful in scenarios where resources are limited or expensive to create.

What Is a Semaphore?

A semaphore in Go is a concurrency control mechanism that limits the number of goroutines accessing a resource simultaneously.

Semaphores can be implemented using buffered channels in Go.

By setting the capacity of the semaphore, you limit how many goroutines can proceed concurrently.

Goroutines acquire the semaphore by sending a value into the channel and release it by receiving from the channel.

Expand your knowledge: Golang Channels

Credit: youtube.com, dotGo 2019 - Valentin Deleplace - Semaphores

To limit the number of concurrent goroutines, you can create a buffered channel semaphore with a capacity of your desired limit.

Here's a step-by-step example of how to use a semaphore:

This approach is particularly useful when you need to process a large number of tasks but want to limit the number of concurrent goroutines to prevent overwhelming the system.

For your interest: Golang Gox

Implementing

Implementing a semaphore in Go can be a bit tricky, but it's not impossible.

A semaphore is a variable that controls the access to a shared resource by multiple processes or threads. In Go, we can implement a semaphore by using a buffered channel. This is because sending to the channel is equivalent to the acquire operation, and receiving from the channel is equivalent to the release operation.

A semaphore is typically implemented using a counter, but a buffered channel makes it easier to implement a semaphore without worrying about the underlying mechanics.

Credit: youtube.com, Golang Semaphore pattern && NoTalk

To implement a semaphore, you can use a buffered channel as the base type. This approach eliminates the need to manually manage a counter and avoids the issues associated with busy-waiting.

Here are some key differences between a semaphore and a buffered channel:

By using a buffered channel, we can avoid the data race and busy-waiting issues that can arise when implementing a semaphore manually.

Usage

The Semaphore type in Go has a very simple API consisting of three methods: Acquire, TryAcquire, and Release. These methods are used to acquire and release the semaphore, with Acquire being blocking and TryAcquire being non-blocking.

Acquire and TryAcquire are used to acquire the semaphore, with Acquire accepting a context so the operation can be cancelled or timeout. Release is used to release the Semaphore after it's been acquired, and calls to release should always be paired with successful acquisitions of the Semaphore to avoid deadlocks.

Credit: youtube.com, Go (Golang) Semaphore Pattern Tutorial

Here's a summary of the Semaphore methods:

  • Acquire: Acquires the semaphore, blocking until it's available.
  • TryAcquire: Attempts to acquire the semaphore, returning immediately if it's not available.
  • Release: Releases the semaphore, making it available for acquisition.

Remember, calls to Release should always be paired with successful acquisitions of the Semaphore to avoid deadlocks.

Types

Golang semaphores have two main types: binary semaphores and counting semaphores.

A binary semaphore can only have a value of 0 or 1, which is useful for controlling access to a single resource.

Counting semaphores, on the other hand, can have any non-negative integer value, making them more versatile for managing multiple resources.

In Golang, binary semaphores are implemented using the mutex type, which provides a simple way to synchronize access to shared resources.

Counting semaphores, however, require a more complex implementation, often involving the use of channels or atomic operations.

In practice, binary semaphores are often used for simple synchronization tasks, while counting semaphores are used for more complex scenarios.

Semaphore Functions

Semaphore Functions provide a way to limit and control concurrent access to a resource. The Acquire function acquires the semaphore blocking until the semaphore/resource is available or the context is done.

Credit: youtube.com, golang semaphore

The TryAcquire function acquires the semaphore without blocking, returning true on success and false on failure. The Acquire method lets the user request for a semaphore token in a blocking way, waiting for one available if all tokens are held by other goroutines.

Here's a brief overview of the Acquire and Release methods:

Note that the Acquire method can return an error if the context is done before the semaphore is acquired. The Release method simply makes a semaphore token available, without blocking.

For another approach, see: Golang Method

New Function

The New function is a crucial part of working with semaphores in Go. It creates a new semaphore with the given concurrency limit.

If you provide a limit value of less than 1, the function will panic. This is a safety feature to prevent common mistakes.

You can use the New function to create a semaphore with a specific limit, allowing you to control the level of concurrency in your program. This is essential for preventing issues like overwhelming the system with too many goroutines, which could lead to resource exhaustion or degraded performance.

Here are some key points to remember when using the New function:

By using the New function, you can create a semaphore that fits your specific needs and helps you manage concurrency effectively in your Go programs.

Func (*Semaphore) Acquire

Credit: youtube.com, Semaphores in Operating System || Process Synchronization

The Acquire method of a Semaphore in Go is a blocking call that allows you to request a semaphore token, waiting until one is available. This method is useful when you need to ensure that a certain number of goroutines can run concurrently.

The Acquire method returns a nil error value on success, indicating that the semaphore was acquired. If the context is done before the semaphore is acquired, ctx.Err() is returned, and the semaphore is left unchanged. This means that if the context is cancelled, the Acquire method will return an error and the semaphore will not be acquired.

Here are some key facts about the Acquire method:

  • It blocks until the semaphore is available or the context is done.
  • It returns a nil error value on success.
  • If the context is done before the semaphore is acquired, ctx.Err() is returned.

In Example 4, we see that the Acquire method is used to acquire a semaphore token in a blocking way. This is useful when you need to ensure that a certain number of goroutines can run concurrently.

Concurrency Limiting

A semaphore is a synchronization construct used in concurrent programming to control access to a shared resource. It acts as a counter that manages the number of threads or goroutines allowed to access the resource simultaneously.

Credit: youtube.com, Mutex vs Semaphore Explained | Concurrency in a Nutshell

To limit the number of concurrent goroutines, you can use a semaphore. This is achieved by using a buffered channel to synchronize goroutines creation with a predefined limit on active goroutines launched by your code. The limit is set by the channel's length.

Here are the semaphore rules:

  • Calling Acquire takes a free slot.
  • If there are no free slots, Acquire blocks the goroutine that called it.
  • Calling Release frees up a previously taken slot.
  • If there are any goroutines blocked on Acquire when Release is called, one of them will immediately take the freed slot and unblock.

What Are Goroutines?

Goroutines are a fundamental concept in concurrency programming, and they're a key part of what makes Go so efficient.

Goroutines are lightweight threads, meaning they have a much smaller footprint than traditional OS threads. This allows you to create thousands of goroutines without exhausting system resources.

To start a goroutine, you simply prefix a function call with the go keyword. This is a syntax that's easy to learn and use, and it's a big part of what makes goroutines so powerful.

Here are some key benefits of using goroutines:

  • Lightweight: Goroutines have a smaller footprint compared to traditional OS threads.
  • Syntax: You start a goroutine by prefixing a function call with the go keyword.

Setup and Teardown

In concurrency limiting, setting up and tearing down semaphores is a crucial step. The Semaphore structure is exposed in the provided code, which includes its setup and tear down.

Credit: youtube.com, Jai Desai - Little's Law: Why Service Concurrency Limits Matter - WTF is SRE 2023

A semaphore is a structure that limits simultaneous access to a shared resource. It's created with a specific size, which determines how many operations can be performed simultaneously.

The NewSemaphore function creates a new semaphore that allows a specified number of operations to occur at the same time. This is done by creating a channel with the specified size and filling it with tokens.

Here's a breakdown of the NewSemaphore function:

The TearDown function is used to tear down a semaphore. If there are any ongoing operations, it will wait for them to complete before proceeding. This is done by draining the channel associated with the semaphore.

The TearDown function works by iterating over the channel and receiving tokens from it. This ensures that all operations have completed before the semaphore is torn down.

Limiting Concurrency

A semaphore is a synchronization construct used in concurrent programming to control access to a shared resource. It acts as a counter that manages the number of threads or goroutines allowed to access the resource simultaneously.

Credit: youtube.com, Vapi Concurrency Limit explained

To implement a semaphore, you can use a buffered channel in Golang. A buffered channel is a type of channel that allows a specific number of elements to be stored in it before blocking the sending operation.

You can limit the number of concurrent goroutines by using a semaphore. For example, you can use a semaphore with a limit of 4 to ensure that no more than 4 goroutines access the external system at the same time.

Here are the semaphore rules:

  • Calling Acquire takes a free slot.
  • If there are no free slots, Acquire blocks the goroutine that called it.
  • Calling Release frees up a previously taken slot.
  • If there are any goroutines blocked on Acquire when Release is called, one of them will immediately take the freed slot and unblock.

The semaphore channel pattern is an effective approach to limit the maximum number of concurrent goroutines. This pattern utilizes a buffered channel to synchronize goroutines creation with a predefined limit on active goroutines launched by your code.

The semaphore channel pattern works as follows:

  • As long as there are slots available in the semaphore channel, the case sem <- true can be selected.
  • Once the sem channel is full, this case can no longer be selected.
  • Once requests begin to complete, the rqDur := <-dur case can be selected.
  • Then, once the request time is sent through the dur channel, the request function finishes and the deferred call with the semaphore channel receiving operation is executed. This releases a slot in the channel, allowing the sem <- true case to be able to be selected again.

Using a semaphore allows us to control the level of concurrency, preventing issues like overwhelming the system with too many goroutines, which could lead to resource exhaustion or degraded performance.

Pattern and Design

Credit: youtube.com, Go Concurrency Concepts - Goroutines, WaitGroups, and Channels

In Go, a semaphore is a synchronization primitive that helps manage access to shared resources by limiting the number of concurrent operations.

A semaphore can be implemented using a mutex and a counter, where the counter represents the available slots.

The semaphore pattern is commonly used in producer-consumer scenarios where multiple producers are writing to a shared buffer and multiple consumers are reading from it.

The semaphore ensures that the producers do not overwhelm the consumers by limiting the number of slots available for writing.

In the example of a semaphore with a capacity of 5, only 5 producers can write to the shared buffer at any given time.

The semaphore pattern can also be used to implement a token bucket algorithm, where tokens represent a certain amount of resources.

The semaphore can be used to limit the rate at which tokens are added to the bucket, preventing overflow.

A semaphore can be implemented using a channel, where the channel acts as the semaphore and the buffer acts as the shared resource.

Worker Pool

Credit: youtube.com, #18 Golang - Building an Efficient Worker Pool: Mastering Concurrency in Go

In the world of concurrency, a worker pool is a common scenario where you have a collection of tasks that need processing, and you want to process them concurrently, allowing at most M tasks to run simultaneously.

You can use a semaphore-based mechanism to avoid traditional worker pool implementation with idling goroutines. This approach is shown in the example code, where a semaphore is created to manage the number of tasks that can run concurrently.

The example code uses a semaphore with a maximum of 10 workers, which means at most 10 tasks can run simultaneously. This is set using the `NewSemaphore` function with the `maxWorkers` parameter.

To create a worker pool, you need to define the number of workers and the tasks to be processed. In the example code, the tasks are represented by the `out` array, which is used to store the results of the factorial operation.

The worker pool is launched using a goroutine that acquires a semaphore token, performs the task, and then releases the token. This is done using the `Acquire` and `Release` methods of the semaphore.

Credit: youtube.com, Конкурентные паттерны на канале с буфером в Go. Worker pool и Semaphore

Here's a summary of the worker pool example:

The worker pool example demonstrates how to use a semaphore to manage the number of tasks that can run concurrently, avoiding traditional worker pool implementation with idling goroutines.

Frequently Asked Questions

Is Golang WaitGroup a semaphore?

Yes, Go's WaitGroup is a counting semaphore used to synchronize goroutines. It allows you to wait for a group of goroutines to finish their execution.

Is semaphore better than mutex?

Semaphores offer more flexibility than mutexes, but the choice between them depends on specific synchronization needs. They're better suited for scenarios where multiple threads need to share resources.

Jeannie Larson

Senior Assigning Editor

Jeannie Larson is a seasoned Assigning Editor with a keen eye for compelling content. With a passion for storytelling, she has curated articles on a wide range of topics, from technology to lifestyle. Jeannie's expertise lies in assigning and editing articles that resonate with diverse audiences.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.