multithreading

std::lock_guard() for a locked std::mutex

社会主义新天地 提交于 2020-12-10 06:46:32
问题 I am new to C++11 threading. The following piece of code should be executed only by the first thread. The other threads (which might race with the first thread) should not enter the locked code area (that's why the std::try_lock() is there). std::mutex mutex; // now ensure this will get called only once per event if (std::try_lock(_mutex) != -1) { return; } { std::lock_guard<std::mutex> guard(_mutex); // critical section } // mutex will be unlocked here (Outside from writing my own lock_guard

How to get the first result from multiple threads and cancel remaining

不想你离开。 提交于 2020-12-08 08:37:19
问题 I have request and multiple threads , which are looking for result in different ways and each thread should get some result at some point.. I need to take the result from the first finished thread , return this result and kill all of the remaining threads . I also have timeout when I'll return some default result.. I can think of two solutions, none of which seems "correct" to me.. 1) Loop through the tasks, ask if they are finished, sleep for a while and return the first finished task it

Pin processor CPU Isolation on Windows

↘锁芯ラ 提交于 2020-12-08 07:19:19
问题 In linux where I work mostly, we use a technique called CPU isolation, that effectively locks a process on to a processor and also prevents the processor from running anything else. Our kernel guys did some magic to handle the interrupts. In windows the closest thing I found is an affinity concept which appears to bind a process/thread to a processor. But it makes no guarantee that the processor is ONLY running that process/thread meaning there can still be context switch and other jitter. Is

Measure java short time running thread execution time

时光怂恿深爱的人放手 提交于 2020-12-08 07:15:05
问题 I'm currently working on some sort of database benchmark application. Basically, what I'm trying to do is to simulate using threads a certain number of clients that all repeat the same operation (example: a read operation) against the database during a certain period of time. During this time, I want, in each thread, to measure the average delay for getting an answer from the database. My first choice was to rely on ThreadMXBean's getThreadCpuTime() method (http://docs.oracle.com/javase/7

Python, Stop a Thread

半世苍凉 提交于 2020-12-07 05:11:21
问题 I'm trying to create a class that pings an ip address and keeps a record for connected/ not connected times. Since this class is a part of a GUI, I wish to stop this thread when asked by user. Found some Q&As regrading this issue, but neither one actually causes thread to stop. I'm trying to make a method, a part of this class that will stop self.run() Here's my Pinger class: class Pinger(threading.Thread): def __init__(self, address='', rate=1): threading.Thread.__init__(self) self.address =

Python, Stop a Thread

末鹿安然 提交于 2020-12-07 05:10:15
问题 I'm trying to create a class that pings an ip address and keeps a record for connected/ not connected times. Since this class is a part of a GUI, I wish to stop this thread when asked by user. Found some Q&As regrading this issue, but neither one actually causes thread to stop. I'm trying to make a method, a part of this class that will stop self.run() Here's my Pinger class: class Pinger(threading.Thread): def __init__(self, address='', rate=1): threading.Thread.__init__(self) self.address =

Python, Stop a Thread

时光怂恿深爱的人放手 提交于 2020-12-07 05:09:31
问题 I'm trying to create a class that pings an ip address and keeps a record for connected/ not connected times. Since this class is a part of a GUI, I wish to stop this thread when asked by user. Found some Q&As regrading this issue, but neither one actually causes thread to stop. I'm trying to make a method, a part of this class that will stop self.run() Here's my Pinger class: class Pinger(threading.Thread): def __init__(self, address='', rate=1): threading.Thread.__init__(self) self.address =

Can one retrieve the return value of a thread function in C++11?

跟風遠走 提交于 2020-12-06 06:58:05
问题 If a function has a non-void return value and I join it using the .join function then is there any way to retrieve its return value? Here is a simplified example: float myfunc(int k) { return exp(k); } int main() { std::thread th=std::thread(myfunc, 10); th.join(); //Where is the return value? } 回答1: You can follow this sample code to get the return value from a thread :- int main() { auto future = std::async(func_1, 2); //More code later int number = future.get(); //Whole program waits for

Is Kotlin `?.let` thread-safe?

有些话、适合烂在心里 提交于 2020-12-05 15:36:29
问题 Is Kotlin ?.let thread-safe? Let's say a variable can be changed in different thread. Is using a?.let { /* */ } thread-safe? If it's equal to if (a != null) { block() } can it happen that in if it's not null and in block it's already null? 回答1: a?.let { block() } is indeed equivalent to if (a != null) block() . This also means that if a is a mutable variable, then: a might be reassigned after the null check and hold a null value when block() is executed; All concurrency-related effects are in

Does seq_cst ordering guarantee immediate visibility?

梦想与她 提交于 2020-12-05 12:45:22
问题 N3243 1.10.21 says It can be shown that programs that correctly use mutexes and memory_order_ seq_cst operations to prevent all data races and use no other synchronization operations behave as if the operations executed by their constituent threads were simply interleaved, with each value computation of an object being taken from the last side effect on that object in that interleaving. This is normally referred to as “sequential consistency”. Does this mean that any seq_cst writes on an