multithreading

Thread safety issue

折月煮酒 提交于 2021-01-28 02:31:47
问题 I have a LinkedList with Objects, that I want to process. Objects get added to it from another thread, but only one Thread removes/reads from it. private LinkedList<MyObject> queue = new LinkedList<>(); new Thread() { @Override public void run() { while (!Thread.interrupted()) { if (!queue.isEmpty()) { MyObject first = queue.removeFirst(); // do sth.. } } } }.start(); In another Thread I add Objects to the queue queue.add(new MyObject()); Sometimes this code leads to an Exception though,

Concurrent.futures code ends up with “BrokenProcessPool”

点点圈 提交于 2021-01-28 02:22:14
问题 Yesterday I asked a similar question and somebody suggested me to use concurrent.futures and asyncio. What I want is skipping the file if it takes too long to process. Here is my code: import os, glob import time import asyncio from concurrent.futures import ProcessPoolExecutor @asyncio.coroutine def coro(loop, of, filename): ex = ProcessPoolExecutor(4) yield from loop.run_in_executor(ex, os.system, "\"C:\\P.exe\" -o {} {}".format(of, filename)) for folder, subfolders, filenames in os.walk

Dining philosophers problem - only 2 thread worked

…衆ロ難τιáo~ 提交于 2021-01-28 02:21:59
问题 I am trying to solve the dining philosophers problem. In my case, every philosopher should eat 1,000,000 times. The problem is that it seems like only "1" and is "3" finished eating. I am using threads with critical section lock, here is my code: CRITICAL_SECTION ghCARITICALSection1; CRITICAL_SECTION ghCARITICALSection2; CRITICAL_SECTION ghCARITICALSection3; CRITICAL_SECTION ghCARITICALSection4; CRITICAL_SECTION ghCARITICALSection5; DWORD WINAPI func(int* phiphilosopher) { if (1 ==

Sharing queue between threads running in different modules

旧城冷巷雨未停 提交于 2021-01-28 02:19:29
问题 I have some modules in different packages for my project. This project requires several threads which might be started in different modules, and I intend to use queues for the inter-thread communication. Is there a way to pass a queue created in one module for use in another module? # ModuleA.py myQueue = Queue.Queue() thread = myThread(threadID1, tName1, myQueue) thread2 = myThread(threadID2, tName2, myQueue) # ModuleB.py myQueue = get_the_previous_queue_created() # possible? thread3 =

Wait for a task (or tasks) to reach certain milestone, the async/await way

柔情痞子 提交于 2021-01-28 02:01:41
问题 There are tons of examples of how to wait for a thread to exit, but sometimes i need to wait just until several threads are "ready" or have reached certain milestone. After reading: What's the proper way to wait for a .NET thread to start up? and http://www.albahari.com/threading/ If i have understood correctly: Wait for one Child Task to be ready : //Main Method var wh = new AutoResetEvent(false); Task childTask = Task.Run(() => ChildTask(wh); wh.WaitOne(); //Child private void ChildTask

Why doesn't my compiler recognize #include <thread> (c++)?

こ雲淡風輕ζ 提交于 2021-01-28 01:51:19
问题 I wrote this as a simplified version of a multithreading example to get a feel for it, but am running into some issues when compiling. My compiler says that thread is not a member of std and prompts me to add #include <thread> to my code even though I have already done so. I've been unable to find any similar problems so far, but I suspect that it is an issue with how I'm compiling it because my code is very similar to the example code. #include <iostream> #include <thread> void doWork () {

Memory visibility when a pointer to an object is passed to std::thread as argument

半腔热情 提交于 2021-01-28 01:26:14
问题 Let's say there is a simple class Counter which contains a integer member count . A new object of Counter is constructed and count is initialized to some number. Now, if I pass a pointer to this object to a new thread as an argument, would the new thread always and instantly see the value that was initialized in the main thread? If so, how is it happening that a different thread is able to see updates made by the main thread without any explicit synchronization? class Counter { public: int

is a Python dictionary thread-safe when keys are thread IDs?

断了今生、忘了曾经 提交于 2021-01-28 00:22:20
问题 Is a Python dictionary thread safe when using the thread ID of the current thread only to read or write? Like import thread import threading class Thread(threading.Thread): def __init__(self, data): super(Thread, self).__init__() self.data = data def run(self): data = self.data[thread.get_ident()] # ... 回答1: If data is a standard Python dictionary, the __getitem__ call is implemented entirely in C, as is the __hash__ method on the integer value returned by thread.get_ident() . At that point

How do you print to console in a Multi-Threaded MEX Function?

巧了我就是萌 提交于 2021-01-28 00:19:17
问题 I'm writing a simple producer consumer MEX function which uses the Boost library. I have manged to get the following program to work without any issues. #include "mex.h" #include <boost/thread/thread.hpp> #include <boost/lockfree/spsc_queue.hpp> #include <iostream> #include <boost/atomic.hpp> int producer_count = 0; boost::atomic_int consumer_count (0); boost::lockfree::spsc_queue<int, boost::lockfree::capacity<1024> > spsc_queue; const int iterations = 10000000; void producer() { for (int i

C# Invoke control from different thread

假装没事ソ 提交于 2021-01-27 23:24:18
问题 I'm working on a server program, which uses multithreading. The problem is, there are multiple classes and alot of threads, which all need access to a certain TextBox. (tbLog) The method (Log) looks like like this: using System; using System.Windows.Forms; using System.ComponentModel; namespace Server { public delegate void Logs(string message); public partial class Menu : Form { public Menu() { InitializeComponent(); } public void Log(string message) { if (this.tbLog.InvokeRequired) this