multithreading

Understanding the example of using std::condition_variable

徘徊边缘 提交于 2020-02-14 08:02:46
问题 There is example of using condition_variable taken from cppreference.com: #include <condition_variable> #include <mutex> #include <thread> #include <iostream> #include <queue> #include <chrono> int main() { std::queue<int> produced_nums; std::mutex m; std::condition_variable cond_var; bool done = false; bool notified = false; std::thread producer([&]() { for (int i = 0; i < 5; ++i) { std::this_thread::sleep_for(std::chrono::seconds(1)); std::lock_guard<std::mutex> lock(m); std::cout <<

Generating a race condition with MRI

孤者浪人 提交于 2020-02-14 05:48:31
问题 I was wondering whether it's easy to make a race condition using MRI ruby(2.0.0) and some global variables, but as it turns out it's not that easy. It looks like it should fail at some point, but it doesn't and I've been running it for 10 minutes. This is the code I've been trying to achieve it: def inc(*) a = $x a += 1 a *= 3000 a /= 3000 $x = a end THREADS = 10 COUNT = 5000 loop do $x = 1 THREADS.times.map do Thread.new { COUNT.times(&method(:inc)) } end.each(&:join) break puts "woo hoo!"

How to wait on WaitHandle without message pumping?

允我心安 提交于 2020-02-14 03:13:26
问题 I want to call WaitHandle.WaitOne(TimeSpan) in .NET, but I'm on the STA thread and it pumps messages while waiting. For reasons that are beyond the scope of this question, I need to wait without pumping. How can I wait for a WaitHandle to be signaled without pumping messages? It seems in some scenarios that WaitHandle.WaitOne does not pump messages. But it does sometimes for some messages. See these links for more information on that: Managed and Unmanaged Threading in Microsoft Windows

Python capture stdout from subprocess line by line

梦想的初衷 提交于 2020-02-13 05:37:36
问题 I've read many of the questions related to this and learned a lot but I still haven't been able to solve my problem. I'm building a wxPython app that runs a c++ executable and displays the stdout from that executable in real time. I've run into several strange results trying to make this work. Here's my current setup/problem: //test.cc (compiled as test.out with gcc 4.5.2) #include <stdio.h> int main() { FILE* fh = fopen("output.txt", "w"); for (int i = 0; i < 10000; i++) { printf("Outputting

How do I get the UI thread's Dispatcher?

社会主义新天地 提交于 2020-02-09 08:57:04
问题 Is there any way to get the UI thread's Dispatcher when you have no reference to any UI elements? 回答1: You can grab the UI Dispatcher from the static application instance: Application.Current.Dispatcher You may want to check Application.Current for null first, as it can be cleared during a shutdown sequence. 回答2: The following works much better for me when being used in a WinForms application that happens to be also using WPF (in my case Esri's Arcmap.exe) private System.Windows.Threading

Methods that Clear the Thread.interrupt() flag

喜欢而已 提交于 2020-02-09 06:49:05
问题 I have recently inherited a large Java Application that has almost no Thread safety in it. What I'm currently working on is getting all of the Threads to correctly handle being interrupted instead of using the very bad Thread.stop() . Part of the problem has been that I do not know every method call out there that clears the interrupt flag. Currently I know that the following will clear the interrupt flag: Thread.interrupted() Thread.sleep(long) Thread.join() Thread.join(long) Object.wait()

How do I ThreadStart a method that is labelled async?

半腔热情 提交于 2020-02-08 10:44:35
问题 I want to run a processing loop on a separate thread: _processingThread = new Thread(new ThreadStart(DoWork))); But DoWork needs to be async: private async Task QueueProcessorDoWork() { while (true) { await something(); } } How can I connect the two together? When I add async Task , it doesn't match the parameter of ThreadStart. It is possible to make the method that sets up the thread async Task , I think, but I am not sure if that would help. What's the best solution here? I need my thread

Implement override for thread Terminate method

回眸只為那壹抹淺笑 提交于 2020-02-08 10:02:47
问题 I am trying to override the virtual TThread::Terminate method, but am finding my override gets called only after the Terminated flag is set. What is the correct way to implement this? TTestThrd = class(TThread) private { Private declarations } protected { Protected declarations } procedure Execute(); override; procedure DoTerminate(); override; public { Public declarations } end; procedure TTestThrd.Execute(); var bTerminated: Boolean; begin // This is the main thread loop try while (not

Multi threading classes not working correctly

坚强是说给别人听的谎言 提交于 2020-02-08 07:37:06
问题 I'm trying to make a program that when the user holds down the left or right arrow key along with the space bar it shoots some red picture boxes that move upwards until the get removed, when I just create one it works fine but when I create two or more the first picture box stops moving and the second one starts moving and if there were three picture boxes then the first and second would stop and the third would start, once the third has finished then the second starts again and then the

How can I fix my audio? (Prioritize)

半城伤御伤魂 提交于 2020-02-08 01:29:26
问题 When two audio's are played at the same time...the sound is canceling out. How do I fix this weird phenomenon? I have some code where there is audio on button click and audio in ten second intervals (in a background service). I have the following code to stop the button audio when the ten second interval plays, and it works fine: public static void myPop(Context context){ AudioManager manager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); if(!manager.isMusicActive()) { /