- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi. I have 2 questions.
1) Atomic as a fence. Writing to an atomic variable has release semantics, e.a. operations before the atomic assigment never move over it. Can I use atomic variables as a fence? Is there any guarantee that compiler won't optimize out the unuzed atomic variable in the following code fragment? Should I qualify it as volatile?
2) Looks like there is a typo in the Tutorial, Chapter 8, Table 13.
x.fetch_and_store(y) does x=y, not y=x.
Thank you,
renorm.
1) Atomic as a fence. Writing to an atomic variable has release semantics, e.a. operations before the atomic assigment never move over it. Can I use atomic variables as a fence? Is there any guarantee that compiler won't optimize out the unuzed atomic variable in the following code fragment? Should I qualify it as volatile?
[cpp]T* ptr; ptr = new T(...); tbb::atomicptr_atomic; ptr_atomic = ptr; /* Past this point ptr should be pointing to the fully constructed instance of T. Right? */[/cpp]
2) Looks like there is a typo in the Tutorial, Chapter 8, Table 13.
x.fetch_and_store(y) does x=y, not y=x.
Thank you,
renorm.
Link Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Why wouldn't you instead load/acquire the fully constructed instance of T from the same atomic, eliminating ptr except perhaps for local use?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Do you mean something like this
[cpp]tbb::atomicThat would work. But the rest of the code uses plain pointers. I can only make the pointer to point to an instance of T.ptr_atomic; ptr_atomic = new T(...); /* past this point ptr_atomic points to the fully constructed instance of T. We can signal all other threads that it is OK to use ptr_atomic. */[/cpp]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In another thread you can pass around a plain pointer after you have read it from the atomic. Would that work?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes it would work. I probably recode the whole thing to use atomic variables. Is there any performance penalty for using atomic pointers instead of plain pointers?
[cpp]tbb::atomicptr_atomic; T* ptr; T t; *ptr_atomic = t; // vs *ptr = t; /* any performance difference? */[/cpp]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Don't replace those plain pointers: use the atomics only for synchronisation moments.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
That makes sense. I changed my code to use atomic to send a message and left the plain pointers unchanged. Looks like everething works well.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page