concurrency

How to execute an action periodically in a GHCJS program?

允我心安 提交于 2019-12-23 09:27:25
问题 Should one use setInterval via Javascript, or use some more idiomatic solution based on threads? 回答1: If you don't care about the motivation, just scroll to my best solution runPeriodicallyConstantDrift below. If you prefer a simpler solution with worse results, then see runPeriodicallySmallDrift . My answer is not GHCJS specific, and has not been tested on GHCJS, only GHC, but it illustrates problems with the OP's naive solution. First Strawman Solution: runPeriodicallyBigDrift Here's my

Is it safe, to share an array between threads?

会有一股神秘感。 提交于 2019-12-23 09:01:30
问题 Is it safe, to share an array between promises like I did it in the following code? #!/usr/bin/env perl6 use v6; sub my_sub ( $string, $len ) { my ( $s, $l ); if $string.chars > $len { $s = $string.substr( 0, $len ); $l = $len; } else { $s = $string; $l = $s.chars; } return $s, $l; } my @orig = <length substring character subroutine control elements now promise>; my $len = 7; my @copy; my @length; my $cores = 4; my $p = @orig.elems div $cores; my @vb = ( 0..^$cores ).map: { [ $p * $_, $p * (

Is an “atomic” interrupt check possible in java?

别来无恙 提交于 2019-12-23 08:44:32
问题 If using following "idiom" with interruption in Java, for example from this answer. while (!Thread.currentThread().isInterrupted()) { try { Object value = queue.take(); handle(value); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } Where take is a blocking operation, can an interrupt not be ignored for the time being if an interrupt "arrives" between the check of Thread.currentThread().isInterrupted() and the call queue.take() ? Is this not a "check-than-act"

Are integer reads atomic in Delphi?

家住魔仙堡 提交于 2019-12-23 08:02:05
问题 For Delphi compilers from XE2 through to XE8, for non-windows target platforms, is the read operation of an integer data member, annotated with [Volatile], atomic? I know for the case of windows platform, it is atomic if and only if the data member is aligned to 4 bytes, but what about non-windows (Android etc.)? Please note, I am not asking about thread-safety. Thread-safety and atomicity are two different things. 回答1: LU RD 's comment is the correct answer. Non-windows works similar to

Why isn’t ReadOnlyDictionary thread-safe?

自古美人都是妖i 提交于 2019-12-23 07:19:26
问题 I’m looking for a readonly-dictionary to be accessed from multiple threads. While ConcurrentDictionary exposes such capabilities, I don’t want to have the overhead and the strange API. .Net 4.5 while providing such a class, the documentation states that only static calls are safe. I wonder why? 回答1: ReadOnlyDictionary is just a wrapper around any other dictionary. As such, it's only as thread-safe as the underlying dictionary. In particular, if there's a thread modifying the underlying

What does it mean when one language is a parallel superset of another?

岁酱吖の 提交于 2019-12-23 07:15:18
问题 I'm reading a journal article about Real-Time Concurrent C, and it mentions in the abstract (so any of you can see the context through that link as well) that "Concurrent C, is a parallel superset of C (and of C++)". Now I know what a superset is, but what do they mean by a "parallel superset" when referring to programming languages? 回答1: They're claiming two things, not one thing that is "modified". It's like saying "a quick red car"; the car is quick and red, it's not having a fast color.

How to setRemoveOnCancelPolicy for Executors.newScheduledThreadPool(5)

巧了我就是萌 提交于 2019-12-23 07:08:36
问题 I have this: ScheduledExecutorService scheduledThreadPool = Executors .newScheduledThreadPool(5); Then I start a task like so: scheduledThreadPool.scheduleAtFixedRate(runnable, 0, seconds, TimeUnit.SECONDS); I preserve the reference to the Future this way: ScheduledFuture<?> scheduledFuture = scheduledThreadPool.scheduleAtFixedRate(runnable, 0, seconds, TimeUnit.SECONDS); I want to be able to cancel and remove the future scheduledFuture.cancel(true); However this SO answer notes that

Is there a way to run C++ Unit Tests tests in parallel?

假如想象 提交于 2019-12-23 07:08:21
问题 I'm using Boost Test for a long time now and I ends up having my tests running too slowly. As each test is highly parallel, I want them to run concurrently with all my cores. Is there a way to do that using the Boost Test Library ? I didn't found any solution. I tried to look a how to write custom test runner, but I didn't much documentation on that point :( If there is no way, does someone know a good C++ Test Framework to achieve that goal ? I was thinking that Google Test would do the job

Concurrent Map with fixed size

两盒软妹~` 提交于 2019-12-23 07:00:54
问题 I need a map with the following requirements : It should be highly concurrent. The put() , get() and remove() methods may be called by multiple threads simultaneously. It should be of fixed size. If the size of the HashMap reaches to the max value (e.g. 10000), addition of a new entry to the map should not be allowed. It CAN'T be LRU cache where the oldest entry gets removed on reaching maximum size. ConcurrentHashMap may satisfy #1. But, not sure how #2 can be implemented on top of

C++11 std::condition_variable: can we pass our lock directly to the notified thread?

一笑奈何 提交于 2019-12-23 06:58:35
问题 I'm learning about C++11 concurrency, where my only prior experience with concurrency primitives was in Operating Systems class six years ago, so be gentle, if you can. In C++11, we can write std::mutex m; std::condition_variable cv; std::queue<int> q; void producer_thread() { std::unique_lock<std::mutex> lock(m); q.push(42); cv.notify_one(); } void consumer_thread() { std::unique_lock<std::mutex> lock(m); while (q.empty()) { cv.wait(lock); } q.pop(); } This works fine, but I'm offended by