Re: How to make this program more efficient?
On 22 Sep., 01:51, Jon Harrop <j...@ffconsultancy.com> wrote:
peter koch wrote:
On 21 Sep., 22:11, Jon Harrop <j...@ffconsultancy.com> wrote:
James Kanze wrote:
On Sep 21, 2:19 pm, "Chris M. Thomasson" <n...@spam.invalid> wrote:
If there's any thread modifying the object, then all accesses
must be synchronized. Period.
Did you know that readers threads can access data-structures
without sync?
Not according to Posix, and not on many machines. Reading
without a sync will probably give you some value, but not
necessarily the last written.
You are assuming that it is necessary to get the most recent value whe=
n,
in fact, it is not.
So you are describing a situation, where a not recent computation (or
no computation at all) is satisfactory.
Yes.
Also you are describing a
situation where the result must be quite small - be representable by
one word.
No. The one word can be a pointer to an arbitrarily-large data structure.
The pointer is mutable and the data structure is typically persistent and
immutable.
I imagine there might be some scenarios where this might be okay, but
it must be a very minor niche that fullfills these properties.
On the contrary, there are a huge number of applications ranging from
high-performance numerics in scientific computing to ordinary databases.
For example, if I post a blog comment just as someone downloads the blog
article it makes no practical difference whether or not they see the late=
st
comment. The only thing that matters is that nobody gets corrupted data
(e.g. half of the old pointer and half of the new pointer) which requires
atomic word-size write but not locks and that updates propagate
sufficiently quickly in practice.
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)?
/Peter