codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Passing non-thread-safe objects through thread-safe containers


By : , Category : c++

The queue ensures thread safety by creating a lock_guard<...> on queue operations. Wouldn't memory consistency of the object being moved between threads be guaranteed since the memory fencing and synchronization would be taken care by the lock_guard?

Yes.

Part of me wants to ensure I am only passing thread-safe objects between threads, but I feel like this case there should be no problem. Is this true?

Yes.

This is essentially why volatile is inapplicable as a device for concurrent data access in C++; it doesn't solve race conditions and, once you've brought concurrency devices (e.g. mutexes) into the fray to fix that, those are also taking care of the memory consistency issue so there's simply nothing left for volatile to do.

ReLated :

It depends on how the object was created and made available to your thread. In general, no, it's not safe, even if the object isn't modified.

Following are some ways to make it safe.

First, create the object and perform any modification that is necessary; you can consider the object to be effectively immutable if no more modifications occur. Then, share the effectively immutable object with other threads by one of the following means:

  • Have other threads read the object from a field that is volatile.
  • Write a reference to the object inside a synchronized block, then have other threads read that reference while synchronized on the same lock.
  • Start the reading threads after the object is initialized, passing the object as a parameter. (This is what you are doing in your example, so you are safe.)
  • Pass the object between threads using a concurrent mechanism like a BlockingQueue implementation, or publish it in a concurrent collection, like a ConcurrentMap implementation.

There might be others. Alternatively, you can make all of the fields of the shared object final (including all the fields of its Object members, and so on). Then it will be safe to share this object by any means across threads. That's one of the under-appreciated virtues of immutable types.

No, according to msdn:

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

You should use concurrent collections (msdn).

BlockingCollection Provides bounding and blocking functionality for any type that implements IProducerConsumerCollection.

ConcurrentDictionary Thread-safe implementation of a dictionary of key-value pairs

ConcurrentQueue Thread-safe implementation of a FIFO (first-in, first-out) queue.

ConcurrentStack Thread-safe implementation of a LIFO (last-in, first-out) stack.

ConcurrentBag Thread-safe implementation of an unordered collection of elements.

IProducerConsumerCollection The interface that a type must implement to be used in a BlockingCollection.

Of course it is crashing. Look at what you are doing: You implement the setter, and the setter calls the setter again. Which calls the setter again, and so on and so on. This has nothing to do with atomic / nonatomic and nothing to do with how you call it. Any call do your setters or getters will crash because they call themselves in unlimited recursion.

BTW. This is careless:

NSLog(@" get self.lastname: %@ ",self.firstName);

BTW. The meaning of atomic / nonatomic properties is discussed in many questions.

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .