C++ Tricky Problem

G

Guest

I have a few global variables and a function that gets called multiple times
to complete a single transaction and uses the variables. The function gets
different notifications. When the function receives a final notification,
that where I need to release the global variables. I don't have control over
when the function gets called because the function is called by the system.
How can I block the function so that it can only be entered after the global
variables are released? Here is my code.

string sHeader;
string sData;

DWORD WINAPI __stdcall MyProc(...)
{
switch (NotificationType)
{
case NOTIFICATION1:
{
// do something here with sHeader and sData
...

break;
}

case NOTIFICATION2:
{
// do something here with sHeader and sData
...

break;
}

case NOTIFICATION3:
{
// release here
...

break;
}
}
}
 
C

Chris Mullins [MVP - C#]

[I'm torn. Is this a programming homework assignment or a real problem? I'll
lean towards the real problem on this one...]

What you're looking for is called Thread Synchronization. If you code is
Managed C++, you want to use the Monitor Class - specifically Monitor.Enter
and Montior.Exit. If you're in Win32 C++, you want to use a Critical
Section.

In both cases, make sure you use try/finally notation to make sure the
Monitor.Exit / LeaveCriticalSection are called even in exception cases.

Since your code looks link Win32 C++ (even though it's posed to a group
where Managed C++ is more common), I think your code will look like:

string sHeader;
string sData;
CriticalSection cs;

void GlobalVarialInitialization()
{
InitializeCriticalSection(&cs);
}

DWORD WINAPI __stdcall MyProc(...)
{
EnterCriticalSection(&cs);
__try
{
switch (NotificationType)
{
...
}
}
__finally
{
LeaveCriticalSection(cs);
}

(It's been a while since i wrote C++ code. Forgive me if I goofed anything
silly...)
 
G

Guest

Chris,

I appreciate your reply. Now, the problem is that the function gets called
many times to work on the global variables to complete a single transaction.
And, if you look at my code, the only location I can leave the critical
section is when NotificationType is SF_NOTIFY_END_OF_SESSION. It would have
to be something like below, but I doubt that it would work. And, I don't
have control over when MyProc() is called or how it's called. I just know
that it can be called many times to process a single request.

CRITICAL_SECTION cs;
string sHeader;
string sData;

DWORD WINAPI __stdcall MyProc(...)
{
EnterCriticalSection(&cs);

switch (NotificationType)
{
case NOTIFICATION1:
{
// do something here with sHeader and sData
...

break;
}

case NOTIFICATION2:
{
// do something here with sHeader and sData
...

break;
}

case SF_NOTIFY_END_OF_SESSION:
{
// release here
...
LeaveCriticalSection(&cs);

break;
}
}
}


--
Be Cool!


Chris Mullins said:
[I'm torn. Is this a programming homework assignment or a real problem? I'll
lean towards the real problem on this one...]

What you're looking for is called Thread Synchronization. If you code is
Managed C++, you want to use the Monitor Class - specifically Monitor.Enter
and Montior.Exit. If you're in Win32 C++, you want to use a Critical
Section.

In both cases, make sure you use try/finally notation to make sure the
Monitor.Exit / LeaveCriticalSection are called even in exception cases.

Since your code looks link Win32 C++ (even though it's posed to a group
where Managed C++ is more common), I think your code will look like:

string sHeader;
string sData;
CriticalSection cs;

void GlobalVarialInitialization()
{
InitializeCriticalSection(&cs);
}

DWORD WINAPI __stdcall MyProc(...)
{
EnterCriticalSection(&cs);
__try
{
switch (NotificationType)
{
...
}
}
__finally
{
LeaveCriticalSection(cs);
}

(It's been a while since i wrote C++ code. Forgive me if I goofed anything
silly...)

--
Chris Mullins

thejackofall said:
I have a few global variables and a function that gets called multiple
times
to complete a single transaction and uses the variables. The function
gets
different notifications. When the function receives a final notification,
that where I need to release the global variables. I don't have control
over
when the function gets called because the function is called by the
system.
How can I block the function so that it can only be entered after the
global
variables are released? Here is my code.

string sHeader;
string sData;

DWORD WINAPI __stdcall MyProc(...)
{
switch (NotificationType)
{
case NOTIFICATION1:
{
// do something here with sHeader and sData
...

break;
}

case NOTIFICATION2:
{
// do something here with sHeader and sData
...

break;
}

case NOTIFICATION3:
{
// release here
...

break;
}
}
}
 
C

Chris Mullins [MVP - C#]

I somehow missed that in your use case.

The Critical Section here will cause you no end of problems if used that
way.

It seems like it's time for you to start on a new internal algorithm....

--
Chris Mullins


thejackofall said:
Chris,

I appreciate your reply. Now, the problem is that the function gets
called
many times to work on the global variables to complete a single
transaction.
And, if you look at my code, the only location I can leave the critical
section is when NotificationType is SF_NOTIFY_END_OF_SESSION. It would
have
to be something like below, but I doubt that it would work. And, I don't
have control over when MyProc() is called or how it's called. I just know
that it can be called many times to process a single request.

CRITICAL_SECTION cs;
string sHeader;
string sData;

DWORD WINAPI __stdcall MyProc(...)
{
EnterCriticalSection(&cs);

switch (NotificationType)
{
case NOTIFICATION1:
{
// do something here with sHeader and sData
...

break;
}

case NOTIFICATION2:
{
// do something here with sHeader and sData
...

break;
}

case SF_NOTIFY_END_OF_SESSION:
{
// release here
...
LeaveCriticalSection(&cs);

break;
}
}
}


--
Be Cool!


Chris Mullins said:
[I'm torn. Is this a programming homework assignment or a real problem?
I'll
lean towards the real problem on this one...]

What you're looking for is called Thread Synchronization. If you code is
Managed C++, you want to use the Monitor Class - specifically
Monitor.Enter
and Montior.Exit. If you're in Win32 C++, you want to use a Critical
Section.

In both cases, make sure you use try/finally notation to make sure the
Monitor.Exit / LeaveCriticalSection are called even in exception cases.

Since your code looks link Win32 C++ (even though it's posed to a group
where Managed C++ is more common), I think your code will look like:

string sHeader;
string sData;
CriticalSection cs;

void GlobalVarialInitialization()
{
InitializeCriticalSection(&cs);
}

DWORD WINAPI __stdcall MyProc(...)
{
EnterCriticalSection(&cs);
__try
{
switch (NotificationType)
{
...
}
}
__finally
{
LeaveCriticalSection(cs);
}

(It's been a while since i wrote C++ code. Forgive me if I goofed
anything
silly...)

--
Chris Mullins

thejackofall said:
I have a few global variables and a function that gets called multiple
times
to complete a single transaction and uses the variables. The function
gets
different notifications. When the function receives a final
notification,
that where I need to release the global variables. I don't have
control
over
when the function gets called because the function is called by the
system.
How can I block the function so that it can only be entered after the
global
variables are released? Here is my code.

string sHeader;
string sData;

DWORD WINAPI __stdcall MyProc(...)
{
switch (NotificationType)
{
case NOTIFICATION1:
{
// do something here with sHeader and sData
...

break;
}

case NOTIFICATION2:
{
// do something here with sHeader and sData
...

break;
}

case NOTIFICATION3:
{
// release here
...

break;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top