multithreading

Enumerator.MoveNext() throws 'Collection was Modified' on first call

坚强是说给别人听的谎言 提交于 2021-02-16 13:33:22
问题 Consider the following code: List<int> list = new List<int>(); IEnumerable<int> enumerable = list; IEnumerator<int> enumerator = enumerable.GetEnumerator(); list.Add(1); bool any = enumerator.MoveNext(); At runtime, the last line throws an: InvalidOperationException: Collection was modified; enumeration operation may not execute. I understand the need for IEnumerators to throw 'Collection was modified' exceptions when the IEnumerable changes, but I don't understand this: Why does the

Why multi-threaded python program slow on ec2 micro-instance?

佐手、 提交于 2021-02-16 09:23:28
问题 I am working on a Online Judge code checker.My code uses multi-threading in python 2.7.The same program on my local machine (i core 3 RAM 4GB) evaluates about 1000 submisions in 1 minute 10 seconds. But when I run it on ec2 micro instance(about 600 MB RAM) it takes about 40 minutes(It gets slow for some random seconds).To know the reason I broke down things. First this is how my evaluator works: I have a main program worker.py , which creates multiple threads The main thread pulls submissions

C++ if one thread writes toggles a bool once done, is it safe to read that bool in a loop in a single other thread?

纵饮孤独 提交于 2021-02-16 08:57:15
问题 I am building a very simple program as an exercise. The idea is to compute the total size of a directory by recursively iterating over all its contents, and summing the sizes of all files contained in the directory (and its subdirectories). To show to a user that the program is still working, this computation is performed on another thread, while the main thread prints a dot . once every second. Now the main thread of course needs to know when it should stop printing dots and can look up a

Using Interlocked.CompareExchange with a class

半世苍凉 提交于 2021-02-16 04:32:50
问题 System.Threading.Interlocked.CompareExchange operator provides atomic (thus thread-safe) C# implementation of the Compare-And-Swap operation. For example int i = 5; Interlocked.CompareExchange(ref i, 10, 5); After this command, the int i would have a value = 10. And also the compare and exchange happens atomically (single operation). When I tried using this with a class instance, the compare fails and the values are not exchanged. public class X { public int y; public X(int val) { y = val; }

Using Interlocked.CompareExchange with a class

∥☆過路亽.° 提交于 2021-02-16 04:32:44
问题 System.Threading.Interlocked.CompareExchange operator provides atomic (thus thread-safe) C# implementation of the Compare-And-Swap operation. For example int i = 5; Interlocked.CompareExchange(ref i, 10, 5); After this command, the int i would have a value = 10. And also the compare and exchange happens atomically (single operation). When I tried using this with a class instance, the compare fails and the values are not exchanged. public class X { public int y; public X(int val) { y = val; }

How to Invoke MessageBox on background thread in VB.NET?

拜拜、爱过 提交于 2021-02-11 18:15:40
问题 How do I call Invoke to run MessageBox.Show on my background thread please? When I display a MessageBox on my single background thread, it usually displays behind the main form, so to make the program usable I need to display it in front of the main form. My current code is MessageBox.Show(myMessageText, myTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation) without owner. This code is not called from the form itself. but from a helper class I have built outside it. I want to add an owner

How to check if caller still exist in task callback

允我心安 提交于 2021-02-11 18:10:12
问题 A very common scenario for a thread's callback is to inform the caller that it has finished his job. Here's the minimal example: class task { public: void operator()(std::function<void()>&& callback) { std::thread t { [c = std::move(callback)]{ std::this_thread::sleep_for(std::chrono::milliseconds{100}); c(); } }; t.detach(); } }; class processor { public: void new_task() { auto& t = tasks.emplace_back(); t([this]{ if (true/*this object still alives*/) finish_callback(); }); } private: void

Why std::async runs functions with a same thread

一笑奈何 提交于 2021-02-11 17:52:50
问题 I was reading an article about concurrent programming with c++ (link). In this article, author shows a code that std::async runs two functions with the same thread. Also, when he used std::future with std::async, it acts differently again and runs all functions with independent threads. Why async behave like that and it has this uncontrolled manner? also, How can I develope a real concurrent program with this feature? is that possible at all or I should miss it? 回答1: I suggests to also read

Why std::async runs functions with a same thread

ⅰ亾dé卋堺 提交于 2021-02-11 17:51:36
问题 I was reading an article about concurrent programming with c++ (link). In this article, author shows a code that std::async runs two functions with the same thread. Also, when he used std::future with std::async, it acts differently again and runs all functions with independent threads. Why async behave like that and it has this uncontrolled manner? also, How can I develope a real concurrent program with this feature? is that possible at all or I should miss it? 回答1: I suggests to also read

Parallel tasks performance in c#

房东的猫 提交于 2021-02-11 17:25:42
问题 I need to make Tasks run faster, I tried to use semaphore, parallel library and threads(tried to open one for every work, I know its the most dumb thing to do), but none of them show the performance I need. I'm not familiar to work with thread stuff and I need some help to find the right way and understand how Task and Threads work. Here is the function: public class Test { public void openThreads() { int maxConcurrency = 500; var someWork = get_data_from_database(); using (SemaphoreSlim