tevino / abool Goto Github PK
View Code? Open in Web Editor NEW:bulb: Atomic Boolean library for cleaner Go code, optimized for performance yet simple to use.
License: MIT License
:bulb: Atomic Boolean library for cleaner Go code, optimized for performance yet simple to use.
License: MIT License
The command go mod download github.com/tevino/abool/v2
fails with the following error :
go: github.com/tevino/abool/[email protected]: go.mod has non-.../v2 module path "github.com/tevino/abool" (and .../v2/go.mod does not exist) at revision v2.0.0
Would it be possible to cut a release tag, so that everyone has a clear version number to point to in their go.mod
?
can we have an additional operation to change the value from true->false and vice versa, atomically?
The following piece of code demonstrates the bug.
I initially added this as a test, but was then unable to solve the bug, using only one atomic function.
func TestSetToIfAfterMultipleToggles(t *testing.T) {
v := New() // false
v.Toggle() // true
v.Toggle() // false
v.Toggle() // true
// As v is true, it should now be flipped to false
v.SetToIf(true, false)
expected := false
if v.IsSet() != expected {
t.Fatalf("Toggling the value atleast 3 times, until it's true, `SetToIf(true, false)` should flip v to false, expected: %v, got %v", expected, v.IsSet())
}
}
This happens, as Toggle
just increases the int32 by +1 every time, but SetToIf
only compares the int32 against 1 and 0 (instead of odd and even).
Hey, I've been using something similar to your atomicbool - adapted from https://gist.github.com/mahan/6256149
sync.Mutex
doesn't allow us to examine state. There are cases where we want to examine a lock and if we cannot acquire it, just return.
Consider the scenario where multiple goroutines call a function, and only one of them needs to proceed. The others can return out immediately (like a http handler that needs to do admin work, what if the request was called multiple times, something to say "already in progress" . this atomic bool is perfect for it ).
func (s *Server) DoSomethingImportant(w http.ResponseWriter, r *http.Request) {
// check if we can set it to true and proceed
canProceed := s.DoSomethingImportantInProgress.TestAndSet()
if !canProceed {
// message that we cannot proceed
return
}
defer s.DoSomethingImportantInProgress.UnSet()
// do the rest...
}
Essentially its just the following code. Would you like me to send it as a PR instead ?
func (ab *AtomicBool) TestAndSet() bool {
return atomic.CompareAndSwapInt32((*int32)(ab), 0, 1)
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.