thread-safety

Has anyone written a thread-safe BindingList<T>?

送分小仙女□ 提交于 2019-12-19 11:39:05
问题 I am currently getting exceptions when modifying an IBindingList on multiple threads. Does anyone have a threadsafe version before I write my own? 回答1: I think you'll find this an incredibly difficult task. The easier path would be to prevent multiple-thread access with a lock : void AddItemToList(object o) { lock(myBindingList) { myBindingList.Add(o); } } Look at the lock statement docs for more info. 回答2: Only just found this post... do you mean like this? 来源: https://stackoverflow.com

Has anyone written a thread-safe BindingList<T>?

可紊 提交于 2019-12-19 11:39:00
问题 I am currently getting exceptions when modifying an IBindingList on multiple threads. Does anyone have a threadsafe version before I write my own? 回答1: I think you'll find this an incredibly difficult task. The easier path would be to prevent multiple-thread access with a lock : void AddItemToList(object o) { lock(myBindingList) { myBindingList.Add(o); } } Look at the lock statement docs for more info. 回答2: Only just found this post... do you mean like this? 来源: https://stackoverflow.com

How can I set a min value in .Net without using a lock?

时间秒杀一切 提交于 2019-12-19 10:19:09
问题 I have multiple threads accessing variables. I know how to write spinlocks and use the Threading.Interlocked methods to increment etc. variables. However, I want to perform the equivalent of: a = Math.Min(a, b) or a = a | 10 ... but without using a critical section. Is this possible? I know the 2nd line is possible in assembler, but there is no Interlocked.Or method. 回答1: Here is the general pattern for simulating an interlocked operation. public static T InterlockedOperation<T>(ref T

WinForms thread safe control access

◇◆丶佛笑我妖孽 提交于 2019-12-19 10:10:57
问题 I get error Control accessed from a thread other than the thread it was created on when I try to access WinForms control. I know that all modifications of control should be performed in UI thread (requiring BeginInvoke() , etc.), but I need my control to be read only. Here is my simplified code: string text = textBox.Text; What is the pattern for accessing WinForms control's properties values from another thread? 回答1: For something as trivial as this, you don't have to use BeginInvoke

Compiler says that data cannot be shared between threads safely even though the data is wrapped within a Mutex

拜拜、爱过 提交于 2019-12-19 09:45:17
问题 I'm using Rocket which has a State that it passes to the HTTP requests. This struct contains a Mutex<DatastoreInstance> which gives access to a SQLite database and is locked with a mutex to make read and writes safe. pub struct DatastoreInstance { conn: Connection, } When the DatastoreInstance struct looked like this, with only a SQLite connection everything worked fine, but I then also wanted to add a transaction object within this struct: pub struct DatastoreInstance { conn: Connection,

Is C read() Thread Safe?

廉价感情. 提交于 2019-12-19 09:27:34
问题 I'm writing a program where multiple threads might read from a file simultaneously. No threads are writing to the file, but they might each copy its contents to separate memory segments. To do implement this, I'm required to use an API that gives me a file descriptor for the file I want to read. I'm reading chunks of the file with C's read function. The man page says that, "On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this

Why does ArrayList not throw ConcurrentModificationException when modified from multiple threads?

一笑奈何 提交于 2019-12-19 09:23:43
问题 ConcurrentModificationException : This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible. Above is ConcurrentModificationException definition from javadoc. So I try to test below code: final List<String> tickets = new ArrayList<String>(100000); for (int i = 0; i < 100000; i++) { tickets.add("ticket NO," + i); } for (int i = 0; i < 10; i++) { Thread salethread = new Thread() { public void run() { while (tickets

Cheapest way of establishing happens-before with non-final field

风流意气都作罢 提交于 2019-12-19 09:07:17
问题 Many questions/answers have indicated that if a class object has a final field and no reference to it is exposed to any other thread during construction, then all threads are guaranteed to see the value written to the field once the constructor completes. They have also indicated that storing into a final field a reference to a mutable object which has never been accessed by outside threads will ensure that all mutations which have been made to the object prior to the store will be visible on

Writing re-entrant lexer with Flex

安稳与你 提交于 2019-12-19 06:59:08
问题 I'm newbie to flex. I'm trying to write a simple re-entrant lexer/scanner with flex. The lexer definition goes below. I get stuck with compilation errors as shown below (yyg issue): reentrant.l: /* Definitions */ digit [0-9] letter [a-zA-Z] alphanum [a-zA-Z0-9] identifier [a-zA-Z_][a-zA-Z0-9_]+ integer [0-9]+ natural [0-9]*[1-9][0-9]* decimal ([0-9]+\.|\.[0-9]+|[0-9]+\.[0-9]+) %{ #include <stdio.h> #define ECHO fwrite(yytext, yyleng, 1, yyout) int totalNums = 0; %} %option reentrant %option

Some clarification on the SyncRoot pattern: what is the correct way to use this pattern?

会有一股神秘感。 提交于 2019-12-19 06:05:40
问题 I read something about the SyncRoot pattern as a general rule to avoid deadlocks. And reading a question of a few years ago (see this link), I think I understand that some uses of this pattern may be incorrect. In particular, I focused on the following sentences from this topic: You’ll notice a SyncRoot property on many of the Collections in System.Collections. In retrospeced, I think this property was a mistake... Rest assured we will not make the same mistake as we build the generic