Skip to content

Commit e156ab6

Browse files
committed
Better examples and snippets in README.md
1 parent e5fd454 commit e156ab6

File tree

1 file changed

+96
-24
lines changed

1 file changed

+96
-24
lines changed

README.md

Lines changed: 96 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,36 @@ The `SystemMutex` is just `sync.Mutex`.
3434
It is also worth observing that the performance started to degrade towards the
3535
very large array sizes. This is most likely due to a cache size limitation.
3636

37+
# Snippets
38+
39+
## Using RTM
40+
41+
```go
42+
m := map[string]int{
43+
"word1": 0,
44+
}
45+
46+
c := NewRTMContexDefault()
47+
c.Atomic(func() {
48+
// Action to be done transactionally
49+
m["word1"] = m["word1"] + 1
50+
})
51+
```
52+
53+
# Using HLE
54+
55+
```go
56+
m := map[string]int{
57+
"word1": 0,
58+
}
59+
60+
var lock safetyfast.SpinHLEMutex
61+
lock.Lock()
62+
// Action to be done transactionally
63+
m["word1"] = m["word1"] + 1
64+
lock.Unlock()
65+
```
66+
3767
# Examples
3868

3969
## Checking for HLE and RTM CPU support
@@ -62,37 +92,79 @@ func main() {
6292
## Using RTM
6393

6494
```go
65-
m := map[string]int{
66-
"word1": 0,
67-
"word2": 0,
68-
"word3": 0,
69-
"word4": 0,
70-
}
95+
package main
7196

72-
c := NewRTMContexDefault()
73-
c.Atomic(func() {
74-
// Action to be done transactionally
75-
count := m["word1"]
76-
m["word1"] = count + 1
77-
})
97+
import (
98+
"fmt"
99+
"sync"
100+
"github.com/linux4life798/safetyfast"
101+
)
102+
103+
func main() {
104+
m := map[string]int{
105+
"word1": 0,
106+
"word2": 0,
107+
}
108+
109+
c := safetyfast.NewRTMContexDefault()
110+
var wg sync.WaitGroup
111+
112+
wg.Add(2)
113+
go c.Atomic(func() {
114+
// Action to be done transactionally
115+
m["word1"] = m["word1"] + 1
116+
wg.Done()
117+
})
118+
go c.Atomic(func() {
119+
// Action to be done transactionally
120+
m["word1"] = m["word1"] + 1
121+
wg.Done()
122+
})
123+
wg.Wait()
124+
125+
fmt.Println("word1 =", m["word1"])
126+
}
78127
```
79128

80129
## Using HLE
81130

82131
```go
83-
m := map[string]int{
84-
"word1": 0,
85-
"word2": 0,
86-
"word3": 0,
87-
"word4": 0,
88-
}
132+
package main
89133

90-
var lock SpinHLEMutex
91-
lock.Lock()
92-
// Action to be done transactionally
93-
count := m["word1"]
94-
m["word1"] = count + 1
95-
lock.Unlock()
134+
import (
135+
"fmt"
136+
"sync"
137+
"github.com/linux4life798/safetyfast"
138+
)
139+
140+
func main() {
141+
m := map[string]int{
142+
"word1": 0,
143+
"word2": 0,
144+
}
145+
146+
var lock safetyfast.SpinHLEMutex
147+
var wg sync.WaitGroup
148+
149+
wg.Add(2)
150+
go func() {
151+
lock.Lock()
152+
// Action to be done transactionally
153+
m["word1"] = m["word1"] + 1
154+
lock.Unlock()
155+
wg.Done()
156+
}()
157+
go func() {
158+
lock.Lock()
159+
// Action to be done transactionally
160+
m["word1"] = m["word1"] + 1
161+
lock.Unlock()
162+
wg.Done()
163+
}()
164+
wg.Wait()
165+
166+
fmt.Println("word1 =", m["word1"])
167+
}
96168
```
97169

98170
[wikipedia-tsx]: https://en.wikipedia.org/wiki/Transactional_Synchronization_Extensions

0 commit comments

Comments
 (0)