thread-safety

Is iterating over a list retrieved in a synchronized block thread-safe?

痴心易碎 提交于 2020-02-04 04:56:51
问题 I am a bit confused regarding one pattern I have seen in some legacy code of ours. The controller uses a map as a cache, with an approach that should be thread safe, however I am still not confident it indeed is. We have a map, which is properly synchronized during addition and retrieval, however, there is a bit of logic outside of the synchronized block, that does some additional filtering. ( the map itself and the lists are never accessed outside of this method , so concurrent modification

Threading - wait()

拜拜、爱过 提交于 2020-01-30 05:41:08
问题 The wait() method on an object can be called only in the synchronized context i.e. the current thread must have a lock on the object to invoke the wait() method. Now if a thread T1 has a lock on an object( obj ) and invokes its wait method obj.wait() . How can other threads get lock on this object( obj ) so that they can also call wait, which is already possessed T1 ? 回答1: wait releases the synchronized context. From the documentation: The current thread must own this object's monitor. The

List of Delphi data types with 'thread-safe' read/write operations?

老子叫甜甜 提交于 2020-01-27 23:55:47
问题 Are 'boolean' variables thread-safe for reading and writing from any thread? I've seen some newsgroup references to say that they are. Are any other data types available? (Enumerated types, short ints perhaps?) It would be nice to have a list of all data types that can be safely read from any thread and another list that can also be safely written to in any thread without having to resort to various synchronization methods. 回答1: Please note that you can make essentially everything in delphi

List of Delphi data types with 'thread-safe' read/write operations?

可紊 提交于 2020-01-27 23:55:46
问题 Are 'boolean' variables thread-safe for reading and writing from any thread? I've seen some newsgroup references to say that they are. Are any other data types available? (Enumerated types, short ints perhaps?) It would be nice to have a list of all data types that can be safely read from any thread and another list that can also be safely written to in any thread without having to resort to various synchronization methods. 回答1: Please note that you can make essentially everything in delphi

ConcurrencyMode.Multiple in stateless WCF services

强颜欢笑 提交于 2020-01-25 21:54:11
问题 We currently have multiple WCF services which are using the default ServiceBehavior . Due to issues with scalability, we are looking at applying the ConcurrencyMode = ConcurrencyMode.Multiple attribute to improve throughput. All of our service calls are completely stateless, for example: PersonService.cs: public class PersonService : IPersonService { public GetPersonResponse GetPerson(GetPersonRequest request) { GetPersonResponse response = new GetPersonResponse(); try { response.Person =

StoreStore reordering happens when compiling C++ for x86

大兔子大兔子 提交于 2020-01-25 20:40:48
问题 while(true) { int x(0), y(0); std::thread t0([&x, &y]() { x=1; y=3; }); std::thread t1([&x, &y]() { std::cout << "(" << y << ", " <<x <<")" << std::endl; }); t0.join(); t1.join(); } Firstly, I know that it is UB because of the data race. But, I expected only the following outputs: (3,1), (0,1), (0,0) I was convinced that it was not possible to get (3,0) , but I did. So I am confused- after all x86 doesn't allow StoreStore reordering. So x = 1 should be globally visible before y = 3 I suppose

Are static inline functions thread safe?

点点圈 提交于 2020-01-25 20:28:07
问题 Scenario: I have written a big piece of code, running on 2 parallel threads, which are identical in term of code and just process different data. I am seeing non-deterministic results. If I disable one of the 2 threads, the results become deterministic. Within this code I am using some static inline functions (main reason: they are small functions that I need here and there, for which I simply duplicate the code in the translation units where they are needed), and I would like to understand

Bring data came from async operation to main thread

我与影子孤独终老i 提交于 2020-01-25 08:43:06
问题 This is a "problem" i have with data that i receive from a library that has some Async operations on sending and receiving data. While i receive data and get data in my Mobile Windows Form or Desktop i need to deal with the cross thread operation. I deal with this while checking InvokeRequired and do Action if true...etc...But, what i really would like is to make this disappear and get my data correctly so i could bind them manipulate etc, without handling this cross-thread problem. Question

A thread-safe holder for arbitrary cloneable data

只愿长相守 提交于 2020-01-25 07:39:06
问题 I have a class SomeMutableData with a public clone() method. I want to make sure, that no thread ever sees an inconsistent state (assuming the instances will be passed around using the holder only). I assume using synchronization is the safest possibility, right? public final class ThreadSafeHolder { public ThreadSafeHolder(SomeMutableData data) { storeData(data); } public synchronized SomeMutableData cloneData() { return data.clone(); } public synchronized void storeData(SomeMutableData data

Synchronization work with conditionals? How can I make this code performant and thread safe?

岁酱吖の 提交于 2020-01-24 23:49:24
问题 Given the following code: public class SomeClass { private boolean shouldBlock = false; private Object resource; public void handleDrawRequest(Canvas canvas) { if (!shouldBlock && resource == null) { shouldBlock = true; loadTheResource(); //which takes awhile shouldBlock = false; } else if (shouldBlock && resrouce == null) { return; //another thread is taking care of the loading of the resource //and its not ready yet, so just ignore this request } drawResourceOn(canvas); } } How can I make