thread-safety

Golang: How to timeout a semaphore?

坚强是说给别人听的谎言 提交于 2019-12-22 07:48:29
问题 Semaphore in Golang is implemented with a channel: An example is this: https://sites.google.com/site/gopatterns/concurrency/semaphores Context: We have a few hundred servers and there are shared resources that we want to limit access to. So for a given resource, we want to use a semaphore to limit access to only 5 concurrent access by those servers. In order to do that, we are planning to use a lock server. When a machine accesses the resource, it will first register with the lock server that

.NET threading like Node.js/V8?

我只是一个虾纸丫 提交于 2019-12-22 07:08:44
问题 I've been away from .NET desktop programming for some time, while drinking the Node.js koolaid. There are some parts of Node.js I find easy to work with. In particular, I like the simplicity of the threading model, and that I can have a few of the benefits of a multithreaded application while only writing code to keep track of a single thread. Now, I have a need to write a multi-threaded application in .NET, and it occurred to me that there is no reason I cannot use a similar threading model

Are memory-mapped files thread safe

╄→尐↘猪︶ㄣ 提交于 2019-12-22 07:05:04
问题 I was wondering whether you could do multithreaded writes to a single file by using memory-mapped files, and making sure that two threads don't write to the same area (e.g. by interleaving fixed-size records), thus alleviating the need for synchronization at the application level, i.e. without using critical sections or mutexes in my code. However, after googling for a bit, I'm still not sure. This link from Microsoft says: First, there is an obvious savings of resources because both

Is createTempFile thread-safe?

对着背影说爱祢 提交于 2019-12-22 07:01:57
问题 I'm using Java 6. Is it possible that two threads calling createTempFile (of the class java.io.File) get the same temp file? 回答1: Best way to get your answer is to look at the source code. At first there isn't any synchronization in createTempFile, but to generate the temp file name, it is using SecureRandom which is ThreadSafe. Then unless you are really unlucky, your file will always get a different name. On top of that, createTempFile implementation is looping, generating new file name,

Lucene QueryParser in multiple threads: synchronize or construct new each time?

无人久伴 提交于 2019-12-22 06:49:05
问题 I have a web application where users submit queries to a Lucene index. The queries are parsed by a Lucene QueryParser. I learned the hard way that QueryParser is not thread-safe. Is it better to use a single QueryParser instance, and synchronize on calls to its parse() method? Or is it better to construct a new instance for each query? (Or would I be better served by a pool of QueryParser s?) I know that in general questions like this depend on the particulars and require profiling, but maybe

How to test if a ThreadLocal has been initialized without actually doing that?

时光毁灭记忆、已成空白 提交于 2019-12-22 06:09:55
问题 I want to test if a ThreadLocal has been initialized without actually initializing it. Of course, the code needs to be thread-safe. Ideally I want something like this: class TestableThreadLocal<T> extends ThreadLocal<T> { public boolean isInitialized() { ... } } But how would I implement this method? Edit: Motivation: I have subclassed a ThreadLocal to override initialValue() . However, I do not always need the initialization, in particular because it could cause a memory leak in multi

Thread-safe execution using System.Threading.Timer and Monitor

喜夏-厌秋 提交于 2019-12-22 06:05:03
问题 Using a System.Threading.Timer results in threads being spun from a ThreadPool , which means if the interval of execution for the timer expires while a thread is still processing by order of a previous request, then the same callback will be delegated to execute on another thread. This is obviously going to cause problems in most cases unless the callback is re-entrant aware, but I'm wondering how to go about it the best (meaning safe) way. Let's say we have the following:

Thread-safe execution using System.Threading.Timer and Monitor

老子叫甜甜 提交于 2019-12-22 06:04:01
问题 Using a System.Threading.Timer results in threads being spun from a ThreadPool , which means if the interval of execution for the timer expires while a thread is still processing by order of a previous request, then the same callback will be delegated to execute on another thread. This is obviously going to cause problems in most cases unless the callback is re-entrant aware, but I'm wondering how to go about it the best (meaning safe) way. Let's say we have the following:

Function local static function-object that's initialized by lambda, thread-safe or not?

让人想犯罪 __ 提交于 2019-12-22 05:42:27
问题 Is the following function thread-safe? And if it's not thread-safe, then is there really any overhead in making that funImpl non-static? Or does the compiler actually inline that function-object function and skip creating the function object altogether? int myfun(std::array<int, 10> values) { static const auto funImpl = [&]() -> int { int sum = 0; for (int i = 0; i < 10; ++i) { sum += values[i]; } return sum; }; return funImpl(); } EDIT: I edited the function signature from: int myfun(const

Function local static function-object that's initialized by lambda, thread-safe or not?

让人想犯罪 __ 提交于 2019-12-22 05:41:11
问题 Is the following function thread-safe? And if it's not thread-safe, then is there really any overhead in making that funImpl non-static? Or does the compiler actually inline that function-object function and skip creating the function object altogether? int myfun(std::array<int, 10> values) { static const auto funImpl = [&]() -> int { int sum = 0; for (int i = 0; i < 10; ++i) { sum += values[i]; } return sum; }; return funImpl(); } EDIT: I edited the function signature from: int myfun(const