thread-safety

Are read from and writing to vector thread-safe operations in vector C++? [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-24 14:14:08
问题 This question already has answers here : std::vector, thread-safety, multi-threading (3 answers) Closed 9 months ago . I have a below code that gives segmentation fault sometime? vector<int> myvector; void function1() { for(int i = 0;i<10;i++) { cout<<"writer thread:"<<i<<endl; myvector.push_back(i); } } void function2() { for(int i = 0;i<10;i++) { cout<<"reader thread:"; cout<<myvector[i]<<endl; } } int main() { thread t1(function1); thread t2(function2); t1.join(); t2.join(); } I am little

Thread safe, Silverlight

时光总嘲笑我的痴心妄想 提交于 2019-12-24 11:44:24
问题 I use very often RIA WCF Services and I inject the same context in several ViewModel. My problem is that as you know, the context of RIA Services, is not thread safe. So I my solution "home made" for synchronization. I use backgrounds workers, and using PostSharp, I apply my attribute [UniqueThread ("Data")] on the method and voila. Do I complicate things? Are there simpler solutions? Best regards, Vincent BOUZON 回答1: In our case we added an OnUiThread method to our BaseViewModel (which also

Multithreading with a global variable: if only one thread is changing the variable, is it necessary to lock it?

人走茶凉 提交于 2019-12-24 09:29:00
问题 As titled, several thread accessing one variable, and only one thread will change the variable, and all the others will just read its value. Like this: Thread 1 : while True: a += 1 Thread 2, 3, 4,... : print a In this case, only Thread 1 is changing variable a . Will any serious problems happen? I found a similar C/C++ tagged question titled "in which cases do I need to lock a variable from simultaneous access?", and it seems from the answer, that the only thing I need to worry about is that

Java selective synchronization

烂漫一生 提交于 2019-12-24 09:02:00
问题 I'm maintaining a very old application, and recently I came across a 'multi thread' bug. Here, in a method, to insert a value into a db, the record is first checked that it exists or not, then if it does not exist, it is inserted into the db. createSomething(params) { .... .... if( !isPresentInDb(params) ) { ..... ..... ..... insertIntoDb(params) } ... } Here when multiple threads invoke this method, two or more threads with same params may cross the isPresentInDb check, one thread inserts

How to make a TParallel.&For loop responsive and store values in a TList<T>?

有些话、适合烂在心里 提交于 2019-12-24 08:46:50
问题 In Delphi 10.1 Berlin I would like to make a TParallel.&For loop responsive. I have a parallel loop similar to the example in question TParallel.For: Store values in a TList while they are calculated in a TParallel.For loop . The loop calculates values and stores these values in a TList<Real> . I try to run the TParallel.&For in a separate thread with TTask.Run to make it responsive: type TCalculationProject=class(TObject) private Task: ITask; ... public List: TList<Real>; ... end; function

Asynchronism in Action Script 3.0

ぐ巨炮叔叔 提交于 2019-12-24 08:29:02
问题 I wonder, how is asynchronism implemented in AS3? Lets take the Timer class. A timer runs asynchronously and dispatches some events. Seems like it creates a new thread for himself. And how are the functions, called when timer event occurs, being thread safe? How is thread safety implemented in AS3? 回答1: AS3 now supports multi threading using "ActionScript Workers" in the latest build. You can see how it is used here in this preview... http://www.bytearray.org/?p=4423 回答2: AS3 [used to be] not

How to make concurrent hash map thread safe with get and put as an atomic operation?

ⅰ亾dé卋堺 提交于 2019-12-24 07:56:50
问题 Is my below method thread safe? This method is in Singleton class. private static final Map<String, PreparedStatement> holder = new ConcurrentHashMap<>(); public BoundStatement getStatement(String cql) { Session session = TestUtils.getInstance().getSession(); PreparedStatement ps = holder.get(cql); if(ps == null) { // If "ps" is already present in cache, then we don't have to synchronize and make threads wait. synchronized { ps = holder.get(cql); if (ps == null) { ps = session.prepare(cql);

Lock-free “decrement if not zero”

做~自己de王妃 提交于 2019-12-24 07:47:46
问题 I'm currently reinventing the wheel of a thread pool in C++. I've eliminated almost all locks from the code, except for multiple instances of the following construct: std::atomic_size_t counter; void produce() { ++counter; } void try_consume() { if (counter != 0) { --counter; // ... } else { // ... } } So, I need a thread-safe lock-free version of this function: bool take(std::atomic_size_t& value) { if (value != 0) { --value; return true; } return false; } There is one solution that I know

Spin Lock Implementations (OSSpinLock)

浪子不回头ぞ 提交于 2019-12-24 07:46:36
问题 I am just starting to look into multi-threaded programming and thread safety. I am familiar with busy-waiting and after a bit of research I am now familiar with the theory behind spin locks, so I thought I would have a look at OSSpinLock's implementation on the Mac. It boils down to the following function (defined in objc-os.h): static inline void ARRSpinLockLock(ARRSpinLock *l) { again: /* ... Busy-waiting ... */ thread_switch(THREAD_NULL, SWITCH_OPTION_DEPRESS, 1); goto again; } (Full

copying data over in objective C from ring buffer in a thread safe manner

半城伤御伤魂 提交于 2019-12-24 07:45:14
问题 I'm puzzled over the result of this code: In one thread I'm writing to the ring buffer (see implementation of ring buffer here): - (void)appendToRingBuffer:(Packet *)packet { int32_t length = ((PacketAudioBuffer *)packet).totalSize; void *writePointer; bytesAvailableToWrite = [ringBuffer lengthAvailableToWriteReturningPointer:&writePointer]; memcpy(writePointer, [((PacketAudioBuffer *)packet).audioBufferData bytes], length); [ringBuffer didWriteLength:length]; //updates ring buffer head