concurrency

TCP accept and Go concurrency model

本秂侑毒 提交于 2019-12-24 10:40:32
问题 Looking at net.TCPListener . One would expect, given the Go concurrency paradigm, for this system functionality to be implemented as a channel, so that you got a chan *net.Conn from a Listen() function, or something similar to that. But it seems Accept() is the way, and that just blocks, just like the system accept. Except it's crippled, because: There is no proper select() you can use with it, because go prefers channels There is no way to set the blocking options for the server sockets. So

Should I RLock map before range?

我的梦境 提交于 2019-12-24 09:38:32
问题 Is it safe to range map without locking if multiple goroutines will run notifyAll func? Actually in a range I need to sometimes remove entries from a map. var mu sync.RWMutex func (self *Server) notifyAll(event *Event) ch := make(chan int, 64) num := 0 for k, v := range self.connections { num++ ch <- num go func(int k, conn *Conn) { err := conn.sendMessage(event) <-ch if err != nil { self.removeConn(k) } }(k, v) } } func (self *Server) removeConn(int k) { mu.Lock() defer mu.Unlock() delete

Means to UPDATE WHERE Value is IN Subquery that has GROUP BY so no Race-Condition Issue(s)?

∥☆過路亽.° 提交于 2019-12-24 09:25:22
问题 Perhaps it's my naiveté, perhaps my paranoia, but I think I'm looking for a solution to a race-condition issue that seems like it should be so common there'd be floods of solutions and I'd've found one by now... but I haven't. The simplistic scenario is I have a process that's supposed to grab any records where there are more than one of a certain type. I'd like to make the system/process(es) thread-/multiprocessing-/reentrant-/buzzword-of-the-day-safe; if the same process gets started and

Race Condition between SELECT and INSERT for multiple columns

允我心安 提交于 2019-12-24 08:59:08
问题 Note: This is a question which is a follow up of this solution. You need to read the link to get context for this question. Also, this is for postgres v9.4 If we want to return multiple columns now instead of just 1 column, how can we achieve it? Let's take a table t: create table t(tag_id int, tag text unique); Now this is what I want: whenever I call a method f_tag_id, I want it to return all the columns for the unique row if it exists in the table t else insert it and return all the

What is a good method to determine when an entities data has been changed on the database?

心已入冬 提交于 2019-12-24 08:58:42
问题 Say, using Entity Framework, I have retrieved an Entity from the database. Is there any way to check subsequently that the same specific Entity has changed in the database by another user? The method I have used previously (in WinForm applications) was to: Save Entity to the db Add an entry to a Transactions table with the Entity type, Unique identifier, and datetime changed When refreshing the same Entity, check for rows in the Transaction table subsequent to the current user's Save. If an

Concurrent Sound on Android Device

ε祈祈猫儿з 提交于 2019-12-24 08:39:23
问题 I have been developing simple drum pads for my phone and have run into a block when it comes to playing two sounds at once (ie during a multitouch event when the user presses down on two or more drum sounds at the same time). For playing the sounds I have been using the SoundPool class. I have a HashMap that hashes a Point to a sound. So my thinking was that when a point is pressed, its corresponding sound will be played. I have a feeling that getting two sounds to play at once involves

Java: How do I use the result of the first of multiple threads that complete?

怎甘沉沦 提交于 2019-12-24 08:32:14
问题 I have a problem in Java where I want to spawn multiple concurrent threads simultaneously. I want to use the result of whichever thread/task finishes first, and abandon/ignore the results of the other threads/tasks. I found a similar question for just cancelling slower threads but thought that this new question was different enough to warrant an entirely new question. Note that I have included an answer below based what I considered to be the best answer from this similar question but changed

How should I guarantee fetch results from a different thread in a nested contexts are up to date, when saves are done asynchronously in background?

混江龙づ霸主 提交于 2019-12-24 07:42:06
问题 I've read the following Behavior differences between performBlock: and performBlockAndWait:? But wasn't able to find an answer to my question. The following code is picked up from an RayWenderlich video. Specifically at 10:05 the code is something like this: class CoreDataStack { var coordinator : NSPersistentStoreCoordinator init(coordinator: NSPersistentStoreCoordinator){ self.coordinator = coordinator } // private, parent, in background used for saving private lazy var savingContext :

Does acquiring a spinlock require compare-and-swap or is swap enough?

亡梦爱人 提交于 2019-12-24 07:37:08
问题 Suppose we have a spinlock implementation: struct Lock { locked : Atomic(bool), } Then an unlock function could be: fun unlock(lock : &Lock) { atomic_store(&lock.locked, false, release); } But what about lock ? Commonly, it uses a compare-and-swap like this: fun lock(lock : &Lock) { while atomic_compare_and_swap(&lock.locked, false, true, acquire) {} } But wouldn't a swap be enough for this? Something like this: fun lock(lock : &Lock) { while atomic_swap(&lock.locked, true, acquire) {} } Is

Web application without SQL query for current user authentication

久未见 提交于 2019-12-24 06:45:12
问题 I have a web server with a lot of requests per second. In order to optimise performances on I/O with the database, the server perform currently only 2 SQL queries by HTTP query: there's one query in order to fetch the current user, there's one query in order to read or write a resource. And I'm currently wondering if it wouldn't be smart to also cache the current user's id, inside a session variable. This way, the server could perform just 1 SQL query by HTTP query. In order to do so, the