Critical Sections & Threading

  • Thread starter Thread starter MPH Computers
  • Start date Start date
M

MPH Computers

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

cheers

Michael
 
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



Here is a VC++ 2003 example of a multithreading application:


__gc class SomeClass
{
int index;

//...

public:

// ...


void DoSomething()
{
Monitor::Enter(this);

// Modify index

Monitor::Exit();
}

void DoSomethingElse()
{
Monitor::Enter(this);

// Modify index

Monitor::Exit();
}

// ...
};


SomeClass *ps= __gc new SomeClass;

// ...

Thread *pthread1= __gc new Thread ( __gc new ThreadStart(ps,
&SomeClass::DoSomething) );



Thread *pthread2= __gc new Thread ( __gc new ThreadStart(ps,
&SomeClass::DoSomethingElse) );


//Start execution of ps->DoSomething()
pthread1->Start();

//Start execution of ps->DoSomethingElse()
pthread2->Start();

// ...
 
Ioannis said:
Here is a VC++ 2003 example of a multithreading application:

Just to clarify - this is an example of a Managed C++ application that uses
the .NET framework and doesn't use Critical Sections.

-cd
 
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()
{
::DeleteCriticalSection(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
 
Thanks for your advice.

I think I understand

I have to create a class inside my form1class, which contains all the code
required to be included in critical section?

the consumers are required to consum only after the complete rotation of
producers has finished, so a new class for each critical section would be
required.

thanks again
 
MPH Computers said:
I have to create a class inside my form1class, which contains all the code
required to be included in critical section?

Despite the fact that I don't understand your question <g>, I'd like to
point out that you need to be thinking about the sections of your
application in which bad things could happen if two or more threads execute
them at the same time.

The rule of thumb is that you hold a "synchronize" threads (here that means
hold the critical section) for as long a period of time as is necessary, but
no longer. Those periods of time _may_ correspond to the lifetime of an
object of a class, or the time it takes to execute a method of a class, or
the time it takes to execute some instructions in a method.

Regards,
Will
 
William DePalo said:
The rule of thumb is that you hold a "synchronize" threads (here that
means hold the critical section) ...

Opps. Make that

The rule of thumb is that you "synchronize" threads (here that means
hold the critical section) ...

Regards,
Will
 
Back
Top