I have Two Threads and a float-type value has been shared between these Threads.
A float-type value is written on Thread1 and reads by Thread2.
Thread1 and Threda2 Start at the same time.
private const int BUFFER_SIZE = 65536 * 8;
private float _readWriteValue = 0;
private bool _stop = false;
private void Thread1()
{
Random ran = new Random();
while (!_stop)
{
_readWriteValue = ran. Next(1, 100);
}
}
private void Thread2()
{
while (!_stop)
{
float[] BufferData = new float[BUFFER_SIZE];
for (int i = 0; i < BUFFER_SIZE; i++)
{
BufferData[i] = _readWriteValue;
}
ProcessMethod(BufferData);
}
}
private void ProcessMethod(float[] data)
{
// Do Something
}
So, When I check BufferData, it fills by only one number. for example, the BufferData fills only with 22. It seems when _readWriteValue goes to for loop in Thread2, has been locked and Thread1 can not write a new value in it.
I’m trying to find out what is the solution. I’m trying by lock, Monitor, and ConcurrentQueue, but every time I get the same results. BufferData is filled by only one number.
Do I have a wrong understanding of the Multi-Threading?
What Should I do?