thread-safety

ThreadAbortException when calling Task.Result

China☆狼群 提交于 2020-08-24 06:26:13
问题 I have the following code where I'm trying to make a request to a remote endpoint using HttpClient : using (var client = new HttpClient()) { client.BaseAddress = _serviceBaseAddress; Task<HttpResponseMessage> readResponseTask = client.GetAsync(relativeUri); readResponseTask.Wait(); using (var response = readResponseTask.Result) { if (response.StatusCode == HttpStatusCode.NotFound || !response.IsSuccessStatusCode) { return default(TResult); } Task<TResult> readContentTask = response.Content

Thread Safety in C

我的梦境 提交于 2020-08-21 02:53:50
问题 imagine I write a library in C. Further, imagine this library to be used from a multi-threaded environment. How do I make it thread-safe? More specific: How do I assure, that certain functions are executed only by one thread at a time? In opposite to Java or C# for example, C has no means to deal with threads/locks/etc., nor does the C standard library. I know, that operating systems support threads, but using their api would restrict the compatibility of my library very much. Which

c# Blocking Collection and Threading

Deadly 提交于 2020-08-08 05:44:08
问题 I'm new to using Blocking Collection and threading and want to make sure I'm following best practice. I'm using a third party API that is not thread safe. I will be making multiple simultaneous requests to the API, so I need to add these requests to a queue and process them one after another. To do this I have a blocking collection: BlockingCollection<myEventArgs> myTasks = new BlockingCollection<myEventArgs>(); private void myEventHandler(object sender, myEventArgs e) { myTasks.Add(e); }

Why does PyQt crashes without information? (exit code 0xC0000409)

牧云@^-^@ 提交于 2020-08-04 09:11:10
问题 I'm trying to develop a software with PyQt, but I often get stuck on software crashes without debug information (only the exit code 0xC0000409). I'm using QThread, and I wrote a system like this: class serialThreadC(QThread): updateOutBox = QtCore.pyqtSignal(str) updateStatus = QtCore.pyqtSignal(int) def __init__(self): super(serialThreadC, self).__init__() self.ser = False self.state = 0 self.serialEnabled = False def run(self): while True: if self.state == -3 or self.state == -2: if self

Is a copy-on-return operation executed prior or after lock_guard destructor? [duplicate]

 ̄綄美尐妖づ 提交于 2020-07-15 04:36:48
问题 This question already has answers here : C++ return value created before or after auto var destruction? (2 answers) in C++ which happens first, the copy of a return object or local object's destructors? [duplicate] (4 answers) Closed 2 years ago . Is the get_a() function safe for race-conditions or do I need to explicitly copy str_ as in get_b() in order to have a thread-safe function? class Class { public: auto get_a() -> std::string { auto&& guard = std::lock_guard{mutex_}; return str_; }

Multithreaded X11 application and OpenGL

别等时光非礼了梦想. 提交于 2020-07-15 03:15:41
问题 I'm trying to create a multithreaded opengl application with libx11 - with one separate thread per window, and one manager thread. I have an event loop in the manager thread: while(true) while(XQLength(mPlatformData->display)){ XNextEvent(mPlatformData->display, &event); std::cout << "event" << std::endl; } } This is a great event loop for single threaded applications, but with this multithreaded setup strange things happen. When I'm creating a window, I need to disable the event queue, or

How to update a Value in a ConcurrentHashMap threadsafe

送分小仙女□ 提交于 2020-07-10 07:06:47
问题 I need to update a value in a ConcurrentHashmap but I am not sure on how to do this thread safe. The Hashmap is a ConcurrentHashMap and I need to get the instance of the custom class, perform some operations on it and then put the updated value back in. Is there any way to combine this get-alter-put operation to something atomic? Thanks in advance 回答1: You can use ConcurrentHashMaps computeIfPresent https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html

How do I share a struct containing a phantom pointer among threads?

一曲冷凌霜 提交于 2020-07-08 12:27:25
问题 I have a structure that needs to be generic over a type, yet the type is not actually contained in the structure: it's used in methods of this structure, not in the structure itself. And so, the structure includes a PhantomData member: pub struct Map<T> { filename: String, phantom: PhantomData<*const T>, } The phantom member is defined as a pointer because the structure does not actually own data of type T . This is per advice in the documentation of std::marker::PhantomData: Adding a field

Are you there, asynchronously written value?

守給你的承諾、 提交于 2020-07-03 06:26:08
问题 The last couple of days I've been reading about async/await. Yesterday I found this video on Channel 9 what made wonder about some things. Please consider the slide below. Aside from the issues Lucian Wischik addresses, I wondered about variable assignment. Say we changed the async void into async Task , and added await before the SendData call. This enables us to get the stream, assign the variable m_GetResponse , wait for two seconds and print it. But what happens to the variable? It could