Skip to content

Commit 6583c66

Browse files
committed
feat: added initial concurrent-safe number
* Added initial concurrent-safe number * Added .gitignore file
1 parent 6f13319 commit 6583c66

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# IDE folders
2+
/.idea
3+
/.vscode
4+
5+
# Environment variables
6+
/**/*.env
7+
8+
# Executable files
9+
/**/*.exe
10+
11+
# Private and public keys
12+
/keys
13+
/**/*.ed
14+
/**/*.pub
15+
/**/*.pem
16+
17+
# Certificates
18+
/certificates
19+
20+
# Commands
21+
/**/*.cmd
22+
/**/*.sh
23+
/**/*.bat
24+
25+
# Service account keys
26+
/iam
27+
/**/service-account.json
28+
29+
# Swagger files
30+
# /docs

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/ralvarezdev/go-concurrency
2+
3+
go 1.23.4

safe_number.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package go_concurrency
2+
3+
import (
4+
"sync"
5+
)
6+
7+
// SafeNumber is a concurrent-safe number
8+
type SafeNumber struct {
9+
mu sync.Mutex
10+
initialValue int
11+
value int
12+
}
13+
14+
// NewSafeNumber creates a new SafeNumber
15+
func NewSafeNumber(initialValue int) *SafeNumber {
16+
return &SafeNumber{value: initialValue, initialValue: initialValue}
17+
}
18+
19+
// Increment increments the number by 1
20+
func (sn *SafeNumber) Increment() {
21+
sn.mu.Lock()
22+
defer sn.mu.Unlock()
23+
sn.value++
24+
}
25+
26+
// Decrement decrements the number by 1
27+
func (sn *SafeNumber) Decrement() {
28+
sn.mu.Lock()
29+
defer sn.mu.Unlock()
30+
sn.value--
31+
}
32+
33+
// GetValue returns the current value of the number
34+
func (sn *SafeNumber) GetValue() int {
35+
sn.mu.Lock()
36+
defer sn.mu.Unlock()
37+
return sn.value
38+
}
39+
40+
// GetValueAndIncrement returns the current value of the number and increments it by 1
41+
func (sn *SafeNumber) GetValueAndIncrement() int {
42+
sn.mu.Lock()
43+
defer sn.mu.Unlock()
44+
sn.value++
45+
return sn.value
46+
}
47+
48+
// GetValueAndDecrement returns the current value of the number and decrements it by 1
49+
func (sn *SafeNumber) GetValueAndDecrement() int {
50+
sn.mu.Lock()
51+
defer sn.mu.Unlock()
52+
sn.value--
53+
return sn.value
54+
}
55+
56+
// OperateValue performs an operation on the value of the number
57+
func (sn *SafeNumber) OperateValue(operation func(int) int) {
58+
sn.mu.Lock()
59+
defer sn.mu.Unlock()
60+
sn.value = operation(sn.value)
61+
}
62+
63+
// GetAndOperateValue performs an operation on the value of the number and returns the result
64+
func (sn *SafeNumber) GetAndOperateValue(operation func(int) int) int {
65+
sn.mu.Lock()
66+
defer sn.mu.Unlock()
67+
sn.value = operation(sn.value)
68+
return sn.value
69+
}
70+
71+
// ResetValue resets the value of the number to the initial value
72+
func (sn *SafeNumber) ResetValue() {
73+
sn.mu.Lock()
74+
defer sn.mu.Unlock()
75+
sn.value = sn.initialValue
76+
}

0 commit comments

Comments
 (0)