multithreading

confused about atomic class: memory_order_relaxed

这一生的挚爱 提交于 2021-02-18 11:41:14
问题 I am studying this site: https://gcc.gnu.org/wiki/Atomic/GCCMM/AtomicSync, which is very helpful to understand the topic about atomic class. But this example about relaxed mode is hard to understand: /*Thread 1:*/ y.store (20, memory_order_relaxed) x.store (10, memory_order_relaxed) /*Thread 2*/ if (x.load (memory_order_relaxed) == 10) { assert (y.load(memory_order_relaxed) == 20) /* assert A */ y.store (10, memory_order_relaxed) } /*Thread 3*/ if (y.load (memory_order_relaxed) == 10) assert

Why is compare-and-swap (CAS) algorithm a good choice for lock-free synchronization?

筅森魡賤 提交于 2021-02-18 11:22:21
问题 CAS belongs to the read-modify-write (RMW) family, a set of algorithms that allow you to perform complex transactions atomically. Specifically, Wikipedia says that CAS is used to implement synchronization primitives like semaphores and mutexes, as well as more sophisticated lock-free and wait-free algorithms. [...] CAS can implement more of these algorithms than atomic read, write, or fetch-and-add, and assuming a fairly large amount of memory, [...] it can implement all of them. https://en

multithreading for data from dataframe pandas

守給你的承諾、 提交于 2021-02-18 11:11:50
问题 I'm struggling to use multithreading for calculating relatedness between list of customers who have different shopping items on their baskets. So I have a pandas data frame consists of 1,000 customers, which means that I have to calculate the relatedness 1 million times and this takes too long to process An example of the data frame looks like this: ID Item 1 Banana 1 Apple 2 Orange 2 Banana 2 Tomato 3 Apple 3 Tomato 3 Orange Here is the simplefied version of the code: import pandas as pd def

Understanding async/await vs Wait in C# with “ContinueWith” behavior

泄露秘密 提交于 2021-02-18 10:46:06
问题 One method is a standard async method, like this one : private static async Task AutoRetryHandlerAsync_Worker(Func<Task<bool>> taskToRun,...) I have tested two implementations, one that use await and the other uses .Wait() The two implementations are not equal at all because the same tests are failing with the await version but not the Wait() one. The goal of this method is to "execute a Task returned by the input function, and retry by executing the same function until it works" (with

Understanding async/await vs Wait in C# with “ContinueWith” behavior

匆匆过客 提交于 2021-02-18 10:46:02
问题 One method is a standard async method, like this one : private static async Task AutoRetryHandlerAsync_Worker(Func<Task<bool>> taskToRun,...) I have tested two implementations, one that use await and the other uses .Wait() The two implementations are not equal at all because the same tests are failing with the await version but not the Wait() one. The goal of this method is to "execute a Task returned by the input function, and retry by executing the same function until it works" (with

Is it safe to pass arguments by reference into a std::thread function?

蹲街弑〆低调 提交于 2021-02-18 09:58:09
问题 #include <thread> #include <string> #include <vector> #include <chrono> using namespace std; void f(const vector<string>& coll) { this_thread::sleep_for(1h); // // Is coll guaranteed to be valid before exiting this function? // } int main() { { vector<string> coll(1024 * 1024 * 100); thread(f, coll).detach(); } // // I know std::thread will copy arguments into itself by default, // but I don't know whether these copied objects are still valid // after the std::thread object has been destroyed

How to listen new db records through java

女生的网名这么多〃 提交于 2021-02-18 08:33:27
问题 Currently I use while(true) and Thread.sleep() for checking for new records in the db and execute java code. Here is an example: public class StartCommands implements Runnable{ private Active_Job activeJob; Runnable execute_command; public StartCommands(){ activeJobs = new Active_Job(); } @Override public void run(){ int jobId = 0; while(true){ //access the db and get one row from the table by the status jobId = activeJobs.get(Status.NEW); if (jobId > 0){ activeJob.updateStatus(Status.INIT);

Purpose of Thread.Sleep(1)?

左心房为你撑大大i 提交于 2021-02-18 08:07:14
问题 I was reading over some threading basics and on the msdn website I found this snippet of code. // Put the main thread to sleep for 1 millisecond to // allow the worker thread to do some work: Thread.Sleep(1); Here is a link to the the page: http://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.80).aspx. Why does the main thread have sleep for 1 millisecond? Will the secondary thread not start its tasks if the main thread is continuously running? Or is the example meant for a task that takes 1

Using futures with boost::asio

空扰寡人 提交于 2021-02-17 21:52:10
问题 Does anyone have a good pointer to examples which use futures from the Boost thread library with Boost ASIO? I have an existing asynchronous library which uses callback function that I would like to provide a friendlier synchronous interface for. 回答1: It is difficult to provide a concise solution without understanding the interactions with the existing asynchronous library. Nevertheless, this answer uses Boost.Future and Boost.Asio to implement an Active Object pattern. When creating a future

Using an atomic read-modify-write operation in a release sequence

☆樱花仙子☆ 提交于 2021-02-17 21:49:06
问题 Say, I create an object of type Foo in thread #1 and want to be able to access it in thread #3. I can try something like: std::atomic<int> sync{10}; Foo *fp; // thread 1: modifies sync: 10 -> 11 fp = new Foo; sync.store(11, std::memory_order_release); // thread 2a: modifies sync: 11 -> 12 while (sync.load(std::memory_order_relaxed) != 11); sync.store(12, std::memory_order_relaxed); // thread 3 while (sync.load(std::memory_order_acquire) != 12); fp->do_something(); The store/release in thread