thread-safety

Sending messages between threads in C#

吃可爱长大的小学妹 提交于 2021-02-07 07:18:17
问题 How can I send and receive messages between threads? 回答1: One solution would be share a concurrent queue, for example (albeit its name) ConcurrentQueue. This will allow you to enqueue an object from one thread and have the other thread (or others threads) dequeue from the queue. As it is a generic solution, you may pass strongly typed items, anything from string to Action will do, or your own custom message class of course. Threre is just one limitation with this approach, the class

Does C++ 11 thread automatically destroy after detach

帅比萌擦擦* 提交于 2021-02-07 05:54:08
问题 Normally, I would assume that C++ 11 thread automatically destroy after detach. But the thing is, I can't find anything to prove this assumption. According to this article Once detached, the thread should live that way forever. forever? If the function of the thread finish, Does its resource remain forever? According to this article After a call to this function, the thread object becomes non-joinable and can be destroyed safely. It can be destroyed safely, but is it automatically? If it's

ReleaseMutex : Object synchronization method was called from an unsynchronized block of code

帅比萌擦擦* 提交于 2021-02-07 02:48:54
问题 I have this pretty straightforward piece of code that very rarely throws "System.ApplicationException : Object synchronization method was called from an unsynchronized block of code." when ReleaseMutex () is called. I logically analyzed the flow of the method and just cannot understand how/why this could happen. To my understanding, the ownership of mutex is guaranteed in this case: readonly string mutexKey; public Logger(string dbServer, string dbName) { this.mutexKey = ServiceManagerHelper

ReleaseMutex : Object synchronization method was called from an unsynchronized block of code

浪子不回头ぞ 提交于 2021-02-07 02:48:45
问题 I have this pretty straightforward piece of code that very rarely throws "System.ApplicationException : Object synchronization method was called from an unsynchronized block of code." when ReleaseMutex () is called. I logically analyzed the flow of the method and just cannot understand how/why this could happen. To my understanding, the ownership of mutex is guaranteed in this case: readonly string mutexKey; public Logger(string dbServer, string dbName) { this.mutexKey = ServiceManagerHelper

Thread-safety of writing a std::vector vs plain array

本小妞迷上赌 提交于 2021-02-06 09:49:27
问题 I've read on Stackoverflow that none of the STL containers are thread-safe for writing . But what does that mean in practice? Does it mean I should store writable data in plain arrays? I expect concurrent calls to std::vector::push_back(element) could lead to inconsistent data structures becaue it might entail resizing the vector. But what about a case like this, where resizing is not involved: using an array: int data[n]; // initialize values here... #pragma omp parallel for for (int i = 0;

Which ADO.NET DataSet/DataTable Methods Are Safe For Multiple Reader Threads?

丶灬走出姿态 提交于 2021-02-02 09:35:22
问题 I am creating a custom cache object for data that is relatively static and is periodically updated from the DB. I have chosen to use a strongly-typed DataSet to store the cached data. Now, access for reading and refreshing (clients cannot write to the cache, only refresh it) to the custom cache object is synchronized via ReaderWriterLockSlim. HOWEVER, I want to ensure that clients of the cache cannot corrupt the data (DataTables, DataRows, etc.) within the strongly-typed data set by

Thread - contention vs race

你离开我真会死。 提交于 2021-01-29 21:21:20
问题 I have seen the terms contention and race are used interchangeably when it comes to thread's state(at critical section). Are they same? 回答1: "Contention" usually refers to the situation where two or more threads need to lock the same lock. We say that the lock is "contested" or maybe, "heavily contested," if there is a significant probability of any thread being forced to wait when it tries to acquire the lock. "Race," "Race condition," and "Data race" are phrases whose meanings have changed

How to use Select-object to get details of process and thread in single query?

隐身守侯 提交于 2021-01-29 15:37:40
问题 when i executed the following query i am not getting the Name of the process only getting the thread details. Get-Process -ProcessName Test | Select-Object @{Label='CMPNAME';Expression={$_.Name}} -ExpandProperty Threads | Select-Object {$_.Id} OUTPUT : $_.Id ----- 8412 10460 10484 10508 10520 10524 回答1: You need to tell Select-Object which properties to select: Get-Process -ProcessName Test | Select-Object @{Label='CMPNAME';Expression={$_.Name}} -ExpandProperty Threads | Select-Object CMPNAME

Thread safe queue with front() + pop()

断了今生、忘了曾经 提交于 2021-01-29 02:20:38
问题 I have created a thread safe queue (see code). The class seems to work but now I want to make the combination front() plus pop() thread safe in such a way that a thread first gets the element and then for sure removes the same element. I can come up with some solutions but they are not elegant for the user side or the strong exception safety guarantee will be lost. The first solution is that the user simply has to lock the ThreadQueueu than call front() and pop() and unlock the ThreadQueue.

What is the difference between runOnUiThread method and Handler? Which one is the best to use?

北城以北 提交于 2021-01-28 20:49:35
问题 I usually use the method runOnUiThread (new Runnable () { @Override public void run () { } }); to launch some prcess in the main thread. just recently I discovered this one new Handler(Looper.getMainLooper()).post(new Runnable () { @Override public void run () { // this will run in the main thread } }); My question is what is the difference between the two methods and which one is the best to use? 回答1: Both are actually same. Both runOnUiThread and Handler#post runs the passed Runnable in the