multithreading

c++ Threads inside for loop print wrong values

拥有回忆 提交于 2020-03-18 03:27:09
问题 I'm trying to understand Multi-threading in c++, but I'am stuck in this problem: if I launch threads in a for loop they print wrong values. This is the code: #include <iostream> #include <list> #include <thread> void print_id(int id){ printf("Hello from thread %d\n", id); } int main() { int n=5; std::list<std::thread> threads={}; for(int i=0; i<n; i++ ){ threads.emplace_back(std::thread([&](){ print_id(i); })); } for(auto& t: threads){ t.join(); } return 0; } I was expecting to get printed

C++ - Multi-threading - Communication between threads

落花浮王杯 提交于 2020-03-17 12:10:13
问题 #include <iostream> #include <thread> #include <condition_variable> #include <queue> #include <cstdlib> #include <chrono> #include <ctime> #include <random> using namespace std; //counts every number that is added to the queue static long long producer_count = 0; //counts every number that is taken out of the queue static long long consumer_count = 0; void generateNumbers(queue<int> & numbers, condition_variable & cv, mutex & m, bool & workdone){ while(!workdone) { unique_lock<std::mutex> lk

Is there any difference between “mutex” and “atomic operation”?

左心房为你撑大大i 提交于 2020-03-17 11:56:29
问题 I'm learning Operating System now, and I'm quite confused with the two concepts - mutex and atomic operation . In my understanding, they are the same, but my OS instructor gave us such a question, Suppose a multi-processor operating system kernel tracks the number of processes created by each user. This operating system kernel maintains a counter variable for each user that it increments every time it creates a new process for a user and decrements every time a process from that user

Java parallel stream: how to wait for threads for a parallel stream to finish?

时光怂恿深爱的人放手 提交于 2020-03-17 10:49:02
问题 So I have a list from which I obtain a parallel stream to fill out a map, as follows: Map<Integer, TreeNode> map = new HashMap<>(); List<NodeData> list = some_filled_list; //Putting data from the list into the map list.parallelStream().forEach(d -> { TreeNode node = new TreeNode(d); map.put(node.getId(), node); }); //print out map map.entrySet().stream().forEach(entry -> { System.out.println("Processing node with ID = " + entry.getValue().getId()); }); The problem with this code is that the

Java parallel stream: how to wait for threads for a parallel stream to finish?

大憨熊 提交于 2020-03-17 10:48:07
问题 So I have a list from which I obtain a parallel stream to fill out a map, as follows: Map<Integer, TreeNode> map = new HashMap<>(); List<NodeData> list = some_filled_list; //Putting data from the list into the map list.parallelStream().forEach(d -> { TreeNode node = new TreeNode(d); map.put(node.getId(), node); }); //print out map map.entrySet().stream().forEach(entry -> { System.out.println("Processing node with ID = " + entry.getValue().getId()); }); The problem with this code is that the

Is multithreading in python a myth?

我们两清 提交于 2020-03-17 08:43:26
问题 To the best of my knowledge, multiple threads can be spawned within the system concurrently but 2 different threads can not access or modify the same resource at the same time. I have even tried many things like creating many threads and putting them in a queue etc. But always I used to hear people say multithreading is not available in Python and that instead you can use multiprocessing to take advantage of multicore CPUs. I this true? Are Python threads only green threads, not real

When to reset CyclicBarrier in java multithreading

半腔热情 提交于 2020-03-13 06:05:30
问题 I was reading CyclicBarrier in the following link http://java-latte.blogspot.in/2013/10/cyclicbarrier-in-java-concurrency.html. In the example 1, CyclicRaceDemo.java main method, CyclicBarrier is being reused without calling reset method. I ran the example and it worked fine. So, I am wondering what's the use of reset method. When should it be called? Or do we need to call it at all? 回答1: A CyclicBarrier is cyclic because it can be reused without resetting. From the Javadoc A synchronization

Should a return statement be inside or outside a lock?

一笑奈何 提交于 2020-03-12 07:05:56
问题 I just realized that in some place in my code I have the return statement inside the lock and sometime outside. Which one is the best? 1) void example() { lock (mutex) { //... } return myData; } 2) void example() { lock (mutex) { //... return myData; } } Which one should I use? 回答1: Essentially, which-ever makes the code simpler. Single point of exit is a nice ideal, but I wouldn't bend the code out of shape just to achieve it... And if the alternative is declaring a local variable (outside

C#, is there such a thing as a “thread-safe” stream?

半城伤御伤魂 提交于 2020-03-11 20:09:51
问题 I am redirecting the output of a process into a streamreader which I read later. My problem is I am using multiple threads which SHOULD have separate instances of this stream. When I go to read this stream in, the threading fudges and starts executing oddly. Is there such a thing as making a thread-safe stream? EDIT: I put locks on the ReadToEnd on the streamreader, and the line where I did: reader = proc.StandardOutput; 回答1: There's a SyncrhonizedStream built into the framework, they just

When to use AtomicReference in Java?

ぃ、小莉子 提交于 2020-03-07 05:31:22
问题 When do we use AtomicReference? Is it needed to create objects in all multithreaded programs? Provide a simple example where AtomicReference should be used. 回答1: Atomic reference should be used in a setting where you need to do simple atomic (i.e. thread-safe, non-trivial) operations on a reference, for which monitor-based synchronization is not appropriate. Suppose you want to check to see if a specific field only if the state of the object remains as you last checked: AtomicReference<Object