Click to See Complete Forum and Search --> : About OOP thread programming


stans
December 30th, 2004, 09:52 PM
Now i am using C++ try to create a Thread class, and used by main function.
in the main function i will create 2 threads and 1 critical section and 1 com port handle, i want the 2 threads share the same handle and critical section.

in the construction of thread, i pass the address of handle and critical section in.
In the function of thread i will try to enter the critical section and do something.
But now the condition i met is that neither of the 2 threads can enter the critical section.
If i don't care about the critical section, neither of them can use the send thing to com port handle.

So is there anyone met this condition before? what's the problem?
Thanks a lot for your help in advance...

Andreas Masur
December 31st, 2004, 05:50 AM
[ Moved thread ]

Andreas Masur
December 31st, 2004, 06:25 AM
Well...re-read you question...would you e able to answer it? In other words, without seeing the code etc. one can only guess what could happen...

radboudp
January 7th, 2005, 06:37 AM
In object orientated programming you place that which belongs together in the same class. So, if you want to use a Com port in a multi threaded environment then I think it would be logical to create a class that allows you to use the Com port that has a critical section to protect multiple threads from calling the functions at the same time....

class CComPort
{
public:
CComPort();
~CComPort();

// Attribs:
public:
private:
HANDLE m_hCom;
CCriticalSection m_lock;

// Methods:
public:
void Send( const BYTE *pBytes, UINT nBytes );
}

CComPort::Send(const BYTE *pBytes, UINT nBytes )
{
m_lock.Lock();

// Do you Com stuff here...

m_lock.Unlock();
}

This function is now guaranteed to be thread secure... See my thread "DeleteCriticalSection causes exception" to get a simple class to handle a critical section...

Good luck,
Radboud