mark_c wrote: ↑Fri Jan 08, 2021 1:28 pm
I ask you once again if you see race condition issue if you were to instantiate 100 threads.
This is not a hard idea to grasp. If multiple threads are sharing a variable, and at least 1 thread writes to that variable while any of the other threads are still making use of the variable, then you must protect the variable from concurrent access. Simple as that.
mark_c wrote: ↑Fri Jan 08, 2021 1:28 pm
Note that bfn [] is only read by threads, never written or modified.
Then there is no race condition.
mark_c wrote: ↑Fri Jan 08, 2021 1:28 pm
Code: Select all
__fastcall TMyThread(int &threadNumber);
Why are you passing the parameter by-reference? Inside the constructor body, you are assigning the parameter to another 'int' variable that is local to the TMyThread object only, thus you are making a copy of the variable's current value. The worker thread will not be accessing the original variable at all, so there is no point in passing it in by-reference if you are just going to copy it anyway. Pass it in by-value instead.
Code: Select all
__fastcall TMyThread(int threadNumber);
mark_c wrote: ↑Fri Jan 08, 2021 1:28 pm
Code: Select all
short *bfn[100];
...
for(int i=0; i<100; i++)
bfn[i] = new short[10];
Since you know the full array size at compile-time, why not just declare it statically instead? It it small enough that it won't take up much memory:
mark_c wrote: ↑Fri Jan 08, 2021 1:28 pm
Code: Select all
void __fastcall TMyThread::Execute()
{
for (int i = 0; i <100; i ++)
bfn[buffer_Num][i];
}
This is a buffer overflow error. The 2nd dimension of the array holds only 10 numbers, but you are accessing 100 from it, which is way out of bounds.
Now, this all being said, you can simply your thread code a little bit:
Code: Select all
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
class TMyThread : public TThread
{
private:
short *buffer;
protected:
void __fastcall Execute();
public:
__fastcall TMyThread(short *buffer);
};
short bfn[100][10];
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
//load data into buffers
bfn[...][i] = ..........;
}
//---------------------------------------------------------------------------
__fastcall TMyThread::TMyThread(short *buffer)
: TThread(false), buffer(buffer)
{
FreeOnTerminate = true;
}
//---------------------------------------------------------------------------
void __fastcall TMyThread::Execute()
{
// I use buffers for read only, never write
for (int i = 0; i < 10; ++i)
buffer[i];
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
for(int i = 0; i < 100; ++i)
{
new TMyThread( &bfn[i] );
Sleep(100);
}
}
//---------------------------------------------------------------------------