peter koch wrote:
On 22 Sep., 01:51, Jon Harrop <j...@ffconsultancy.com> wrote:
The pointer is mutable and the data structure is typically persistent
and immutable.
But I do not see how you avoid the problem that half of the result has
been propagated to the other thread together with the new pointer
value, but the other half of the result has not?
struct result
{
int value;
int result;
};
result result_vec[2] = { result(1,1), result(0,0) };
// global
result* pres = &result_vec[0];
// thread 1:
result temp(2,func(2));
result[1] = temp;
pres = result + 1;
//thread 2:
result* local = pres;
std::cout << "Result: " << *local;
What prevents thread 2 to output e.g. a result(2,0)?
You are representing the value as an unboxed mutable struct. You need to
represent it as a mutable pointer to an immutable data structure:
struct result { const int value, result; };
To update, you write a single "result *".
Immutable data structures can be shared between threads safely. However,
they cannot be implemented efficiently without a performant run-time
(allocator and GC) so this is not feasible in C++.
I still don't get it. Are you telling me that we if we changed to your
pointer (and nothing more).