MPH said:
Hi I am looking for some help
on Threading and Critical Sections
I have a main thread that controls an event
the event handler creates a new thread for carrying out the work
because the work may not be completed before the event is triggered
again
I am trying to add critical sections round the producer & consumer
parts of the work
What I am actually looking for is the VC++ syntax for creating the
thread & critical sections
It keeps asking for a second parameter for the theadstart, which is
not needed in C#
and finally the critical section return with not a class or namespace
if anybody can help I would be really grateful
Some comments:
1. Creating a new thread to handle each event is probably not the best
solution. Creating and destroying threads is expensive. If you have an
event that occurrs irregularly, or an event whose processing may sometimes
(but not too often!) take longer than the inter-event time, you're better
off creating a producer/consumer queue. Create a single worker thread that
reads events from the queue and process them, while your main thread does
whatever processing generates (or detects, receives, etc) the events.
2. To create a thread in a native C++ application (not using the .NET
framework), you should use _beginthread or _beginthreadex. Look these up on
MSDN. Both of these functions expect you to pass a pointer to a non-member
(or static member) function with the appropriate signature. Make sure you
compile with one of the Multi-Threaded options in the Code Generation
section of project properties, or these functions will not be accessible.
You can look these functions up in MSDN.
3. The CriticalSection API is very simple, consisting of only 4 or 5
functions (depending which OS version(s) you're targeting).
CRITICAL_SECTION - this is a struct that contains the critical section data.
Define an instance of this struct at an appropriate scope for your
application (it might be a class member or a namespace scoped variable -
just make sure it has a long enough lifetime to outlive anything that's
trying to use it).
InitializeCriticalSection - you must call this oncee for each critical
section that you use, before calling any other function.
DeleteCriticalSection - you should call this once for each critical section
that you use, after calling any other functions. If you don't call this,
you'll be leaking resources (not really a problem if your program is going
to terminate, but it would be a problem if you're allocating CS's in a loop
and not deleting them). Note that DeleteCriticalSection does NOT reclaim
any memory occupied by the critical section - it simply reclaims the system
resources owned by the Critical Section.
EnterCriticalSection - analogous to Monitor.Enter()
LeaveCriticalSection - analogous to Monitor.Exit().
There's also TryEnterCriticalSection, which won't block if the critical
section is already owned (and isn't supported pre windows 2000, if I recall
correctly - check MSDN).
It's common practice to wrap the CRITICAL_SECTION structure in a class, such
as:
struct MyCriticalSection : CRITICAL_SECTION
{
MyCriticalSection()
{
::InitializeCriticalSection(this);
}
~MyCriticalSection()
{
:
eleteCriticalSection(this);
}
void Enter()
{
::EnterCriticalSection(this);
}
void Leave()
{
::LeaveCriticalSection(this);
}
};
It's also common practice to make an "RAII" (google it) to use with the
critical section class:
class MyCSLock
{
MyCriticalSection& m_cs;
public:
MyCSLock(MyCriticalSection& cs) : m_cs(cs)
{
m_cs.Enter();
}
~MyCSLock()
{
m_cs.Leave();
}
};
The lock class is used as follows
MyCriticalSection csQueue; // CS to protect queue
void WriteToQueue( /* whatever parameters */)
{
MyCSLock lock(csQueue);
// Write the message to the queue
// The destructor of 'lock' will automatically leave the critical section
}
void ReadFromQueue(/* whatever the parameters are */)
{
MyCSLock lock(csQueue);
// Read a message from the queue
// The destructor of 'lock' will automatically leave the critical section
}
Similar to Critical Sections, it's not uncommon to create a class to serve
as a wrapper for the Threading API. Typically something that models a subset
of the Java or .Net Thread class is implemented. I leave that as an
exercise for the reader (or do some googling - there are surely dozens of
variants out there in the wild).
Finally, MFC contains classes that encapsulate the use of threads, critical
sections, and many other aspects of native windows programming, so you might
want to look into those (although MFC does have a bit of a learning curve).
-cd