473,394 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Critical Sections & Threading

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
Nov 17 '05 #1
6 3238
MPH Computers wrote:
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();

// ...
Nov 17 '05 #2
Ioannis Vranos wrote:
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
Nov 17 '05 #3
MPH Computers wrote:
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

Nov 17 '05 #4
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
"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:OT****************@TK2MSFTNGP09.phx.gbl...
Ioannis Vranos wrote:
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

Nov 17 '05 #5
"MPH Computers" <MP**********@hotmail.com> wrote in message
news:O0*************@tk2msftngp13.phx.gbl...
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
Nov 17 '05 #6
"William DePalo [MVP VC++]" <wi***********@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the...
2
by: Xarky | last post by:
Hi, I am trying to learn Critical Sections. I have written a small program. Source code problem below. What the program is doing is disabling the CRTL-C signal in the critical section. My...
7
by: Piotrek Stachowicz | last post by:
Hi, I need to create the situation in my system, where no more critical sections can be initialized (win2000Server). I thought about creating a simple c# application and using the Monitor class....
13
by: Hendrik van Rooyen | last post by:
Hi, I would like to do the following as one atomic operation: 1) Append an item to a list 2) Set a Boolean indicator It would be almost like getting and holding the GIL, to prevent a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.