thread-safety

Update UI from background Thread

隐身守侯 提交于 2019-12-29 08:23:10
问题 This is just a curious question. Which one is the best way to update UI from another thread. First, this one: private delegate void MyDelegateMethod(); void MyMethod() { if (unknowncontrol.InvokeRequired) { this.BeginInvoke(new MyDelegateMethod(MyMethod)); return; } unknowncontrol.property = "updating!"; } On the other hand: Invoke((System.Threading.ThreadStart)delegate() { unknowncontrol.property = "updating!"; }); Or, is there a better way to do this? Of course this is for WinForms, for WPF

Execute a delegate in the ui thread (using message pump)

。_饼干妹妹 提交于 2019-12-29 07:59:13
问题 I have a background thread that handles communication with an external service. Each time the background thread receives a message I'd like to pass it to the UI thread for further processing (displaying to user). Currently I've made a thread safe message queue that is pooled periodically in Timer.Tick and filled in the background thread. But this solution is sub optimal. Do you know how to use message pump to pass events from background thread to ui thread? 回答1: There are a few techniques.

Is it thread safe to call printf in threads that run simultaneously? [duplicate]

余生长醉 提交于 2019-12-29 07:33:04
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: stdout thread-safe in C on Linux? Say thread1 and thread2 are similar and at the end of their jobs they both printf. Is it thread safe or do they have to lock printf somehow? Is it related to stdout? What if one does fflush(stdout) after each printf? Does it change anything? 回答1: The POSIX.1 and C-language functions that operate on character streams (represented by pointers to objects of type FILE) are required

C++ communication between threads

心已入冬 提交于 2019-12-29 06:43:11
问题 I have a couple classes that each open a different program in different threads and do/hold information about it using CreateProcess (if there's a more C++ oriented way to do this let me know-- I looked). Some of the classes are dependent on one of the other programs running. ie B must stop if A stopped. I made this code a while ago and my solution then, was having a class with static functions that run the various programs and static member variables that hold their "state". I was also using

Is memcpy process-safe?

六月ゝ 毕业季﹏ 提交于 2019-12-29 06:19:59
问题 Ive looked online and have not been able to satisfy myself with an answer. Is memcpy threadsafe? (in Windows) What I mean is if I write to an area of memory shared between processes (using boost::shared_memory_object) using a single memcpy and then try read that area from another process using a single memcpy then will one process be blocked automatically while that write is happening? Where can I read about this? 回答1: memcpy is typically coded for raw speed. It will not be thread safe. If

Thread-safety of System.Timers.Timer vs System.Threading.Timer

风流意气都作罢 提交于 2019-12-29 03:54:08
问题 In this article: http://msdn.microsoft.com/en-us/magazine/cc164015.aspx the author states that System.Threading.Timer is not thread-safe . Since then this has been repeated on blogs, in Richter's book "CLR via C#", on SO, but this is never justified. Moreover the MSDN documentation assures "This type is thread safe." 1) Who tells the truth? 2) If this is the original article what makes System.Threading.Timer not thread-safe and how its wrapper System.Timers.Timer achieves more thread-safety?

Is the null coalesce operator thread safe?

心不动则不痛 提交于 2019-12-28 06:27:15
问题 So this is the meat of the question: Can Foo.Bar ever return null? To clarify, can '_bar' be set to null after it's evaluated as non-null and before it's value is returned? public class Foo { Object _bar; public Object Bar { get { return _bar ?? new Object(); } set { _bar = value; } } } I know using the following get method is not safe, and can return a null value: get { return _bar != null ? _bar : new Object(); } UPDATE: Another way to look at the same problem, this example might be more

Is C#'s using statement abort-safe?

纵饮孤独 提交于 2019-12-28 05:49:31
问题 I've just finished reading "C# 4.0 in a Nutshell" (O'Reilly) and I think it's a great book for a programmer willing to switch to C#, but it left me wondering. My problem is the definition of using statement. According to the book (p. 138), using (StreamReader reader = File.OpenText("file.txt")) { ... } is precisely equivalent to: StreamReader reader = File.OpenText("file.txt"); try { ... } finally { if (reader != null) ((IDisposable)reader).Dispose(); } Suppose, however, that this is true and

May volatile be in user defined types to help writing thread-safe code

五迷三道 提交于 2019-12-28 05:01:06
问题 I know, it has been made quite clear in a couple of questions/answers before, that volatile is related to the visible state of the c++ memory model and not to multithreading. On the other hand, this article by Alexandrescu uses the volatile keyword not as a runtime feature but rather as a compile time check to force the compiler into failing to accept code that could be not thread safe. In the article the keyword is used more like a required_thread_safety tag than the actual intended use of

Are C# structs thread safe?

大憨熊 提交于 2019-12-28 04:09:07
问题 Is a C# struct thread-safe? For example if there is a: struct Data { int _number; public int Number { get { return _number; } set { _number = value; } } public Data(int number) { _number = number; } } in another type: class DadData { public Data TheData { get; set; } } is property named TheData, thread-safe? 回答1: No, structures in .NET are not intrinsically thread-safe. However, the copy-by-value semantics that structures have great relevance to this converation. If you are passing your