C++ Tricky Problem

C++ Tricky Problem

am 02.11.2007 02:31:01 von thejackofall

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;
}
}
}




--
Be Cool!

Re: C++ Tricky Problem

am 02.11.2007 02:50:31 von Chris Mullins

[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" wrote in message
news:FDC50248-6F5A-4398-8C66-CE55AC27A4F7@microsoft.com...
>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;
> }
> }
> }
>
>
>
>
> --
> Be Cool!

Re: C++ Tricky Problem

am 02.11.2007 19:11:02 von thejackofall

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 [MVP - C#]" wrote:

> [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" wrote in message
> news:FDC50248-6F5A-4398-8C66-CE55AC27A4F7@microsoft.com...
> >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;
> > }
> > }
> > }
> >
> >
> >
> >
> > --
> > Be Cool!
>
>
>

Re: C++ Tricky Problem

am 02.11.2007 19:37:21 von Chris Mullins

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" wrote in message
news:84F688C8-59FD-48A6-BA52-D3F4FA3E056E@microsoft.com...
> 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 [MVP - C#]" wrote:
>
>> [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" wrote in message
>> news:FDC50248-6F5A-4398-8C66-CE55AC27A4F7@microsoft.com...
>> >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;
>> > }
>> > }
>> > }
>> >
>> >
>> >
>> >
>> > --
>> > Be Cool!
>>
>>
>>