To implement a concurrent stack data structure using sync
package in Golang, follow these steps:
sync.Mutex
to ensure mutual exclusion while accessing the stack.type ConcurrentStack struct {
data []interface{}
lock sync.Mutex
}
Push
, Pop
, and IsEmpty
. Use the sync.Mutex
lock and unlock methods to ensure mutual exclusion.func (s *ConcurrentStack) Push(item interface{}) {
s.lock.Lock()
defer s.lock.Unlock()
s.data = append(s.data, item)
}
func (s *ConcurrentStack) Pop() (interface{}, bool) {
s.lock.Lock()
defer s.lock.Unlock()
if len(s.data) == 0 {
return nil, false
}
item := s.data[len(s.data)-1]
s.data = s.data[:len(s.data)-1]
return item, true
}
func (s *ConcurrentStack) IsEmpty() bool {
s.lock.Lock()
defer s.lock.Unlock()
return len(s.data) == 0
}
ConcurrentStack
and use it concurrently in your code.func main() {
stack := ConcurrentStack{}
wg := sync.WaitGroup{}
// Pushing items concurrently
for i := 0; i < 10; i++ {
wg.Add(1)
go func(item int) {
stack.Push(item)
wg.Done()
}(i)
}
wg.Wait()
// Popping items concurrently
for !stack.IsEmpty() {
wg.Add(1)
go func() {
item, _ := stack.Pop()
fmt.Println(item)
wg.Done()
}()
}
wg.Wait()
}
In this example, 10 goroutines are used to concurrently push items onto the stack. Then, another set of goroutines pop items until the stack is empty, printing each item. The sync.WaitGroup
is used to wait for all goroutines to finish before exiting the program.
By using a sync.Mutex
to protect the critical sections, the stack operations are executed safely in concurrent environments.