A
Andrew FPGA
Guest
Hi,
Its not clear to me when I need to protect class members, to avoid
(often subtle) multithreading bugs.
Say I have thread A writing a class member from time to time. And then
thread B reading that same class member from time to time. Thread A
and B are completly unsynchronised with respect to each other. Do I
really need to use a semaphore to protect access to the variable? Can
the class member be corrupted if the write and read occur "at the same
time"?
Here is another example.
Thread A ...
....
wait (this.bucket >= num_tokens);
....
Then Thread B...
....
this.bucket++; //eventually this will trigger the thread A wait
....
Is it safe to wait on an expression involving a class member that is
written by another thread?
I use semaphores, mailboxes, etc somewhat often. But I wonder
sometimes if its really necessary, but I do it because I'm paranoid
about multithreading bugs. But then it precludes doing things like the
wait example above, which would otherwise be quite neat, if I was
confident it wasn't going to burn me with a subtle bug later.
Cheers
Andrew
Questa 6.6.
Its not clear to me when I need to protect class members, to avoid
(often subtle) multithreading bugs.
Say I have thread A writing a class member from time to time. And then
thread B reading that same class member from time to time. Thread A
and B are completly unsynchronised with respect to each other. Do I
really need to use a semaphore to protect access to the variable? Can
the class member be corrupted if the write and read occur "at the same
time"?
Here is another example.
Thread A ...
....
wait (this.bucket >= num_tokens);
....
Then Thread B...
....
this.bucket++; //eventually this will trigger the thread A wait
....
Is it safe to wait on an expression involving a class member that is
written by another thread?
I use semaphores, mailboxes, etc somewhat often. But I wonder
sometimes if its really necessary, but I do it because I'm paranoid
about multithreading bugs. But then it precludes doing things like the
wait example above, which would otherwise be quite neat, if I was
confident it wasn't going to burn me with a subtle bug later.
Cheers
Andrew
Questa 6.6.