Volatile list iterators

Hi,
I'm trying to get my volatile list iterator working, but I'm not able to assign it to another list iterator.

Here's my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

class MyClass
{
 
  // some other stuff 

  struct Chunk
  {
    int : sampleData;
  }

  volatile std::list<Chunk>::iterator  _currentWriteChunk;
  std::list<Chunk>::iterator _currentReadChunk;
  int _currentWriteIndex;
  int _currentReadIndex;
}

void MyClass::someFunction
{
 // get a copy of the currentWriteChunk 
 // the next line doesn't compile
 // but it does compile if I remove the volatile from _currentWriteChunk's declaration
 // If I add volatile to the next line it doesn't help
 std::list<MyClass::Chunk>::iterator writeChunkToUse ( _currentWriteChunk );

}


VisualStudio 2005 gives me the error code C2558 (no copy constructor or copy constructor declared "explicit")

Btw. it's no problem assigning a volatile int to a non volatile and vice versa.

What I basically want to do is to create a fifo buffer which is written to by one thread and read from another one.
That's why I wanted to make the iterator volatile.

Anybody got a clue?

Thanks
onur

I've never worked with volatile objects, but I think this is what const_cast is for.
http://www.cppreference.com/wiki/keywords/const_cast
There are two problems using the const_cast:
* const_cast needs a reference or pointer as a type
* if you cheat with volatile there is no point in using it anyway
const_cast needs a reference or pointer as a type

Doesn't this work?

*const_cast<std::list<MyClass::Chunk>::iterator*>(&_currentWriteChunk);


if you cheat with volatile there is no point in using it anyway

writeChunkToUse won't be a volatile variable, I don't see where you cheat with it, but again I've never used volatile, neither const_cast.
This const_cast works but only if they have the same layout in memory.

But now new errors pop out on other places.

Now I can't use these iterators any more to access the element of the struct "Chunk".

I think I'll stick to the non volatile version as long as I don't know why it fails...

Thanks for the hint with const_cast anyway!
Declaring an instance of a list<>::iterator as volatile is rather pointless. It is a complex object that the
compiler will not store in registers and elide reads/writes from/to memory.

Besides, if one thread only ever accesses the currentWriteChunk iterator and the other only ever
accesses the currentReadChunk iterator, then there is no collision at that level, so there is no need
even for any synchronization.

If you find a need for synchronization, then "volatile" will not help you; you will need to use
mutexes, etc.

What I tried to avoid was using a mutex etc.

I wanted to declare them volatile so that the reading thread updates the writePointer when it check's if there is new data.

But it was stupid because the write pointer could have been partially updated by the writing thread and then read by the reading thread.

I now made a singly linked list (using volatile raw pointers) by myself and this works without any mutex as long as only one thread writes and one thread reads.

I would have preferred an already tested solution but couldn't find a lightweight solution.
Topic archived. No new replies allowed.