Click to See Complete Forum and Search --> : CWinThread and Vars


Karsten Döring
May 27th, 1999, 03:34 AM
Hi,

I have several Threads of following class running:

class CThreadTest : public CWinThread
{
public:
DECLARE_DYNCREATE( CThreadTest )

virtual BOOL InitInstance();
virtual int Run();

char recv_Data[1024];
}




Everything works fine, but I want to know, if recv_Data is local on each thread? That means if I modify recv_Data on each Thread at the same time, do I change it in every Thread and is it in every Thread the same then?

I hope u understand my problem.

Thanx in advance.

Karsten

May 27th, 1999, 04:12 AM
Unless you are doing something REALLY peculiar, the answer is "YES": each thread and it's associated object will have a local copy of recv_Data[]. Changing the value of recv_Data[] in one thread will NOT affect the value in any other threads.

Cheers!
Humble Programmer
,,,^..^,,,

Karsten Döring
May 27th, 1999, 04:16 AM
Hi,

why is my code a bit strange? can u tell me please?

May 27th, 1999, 04:54 AM
Your code is find...don't read anything into my previous posting. One word of advice, however: don't hardcode "magic numbers" in your code, because it makes it harder to maintain. May I suggest:

const kBufferSize = 1024;

class CTestThread : public CWinThread
{
...
BYTE m_recvData[kBufferSize];
...
};



Cheers!
Humble Programmer
,,,^..^,,,

Jörg Eckart
May 27th, 1999, 05:11 AM
Hi,

before you can run your thread you have to create an instance of your class CThreadTest. That means that the members are local for each instance. Only static members are shared between the instances. But be careful with shared data and keep assured that concurrency access is blocked e.g. with a mutex object.

Greetings, Jörg