473,626 Members | 3,216 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ Tricky Problem

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 (NotificationTy pe)
{
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!
Nov 2 '07 #1
3 1635
[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 / LeaveCriticalSe ction 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 GlobalVarialIni tialization()
{
InitializeCriti calSection(&cs) ;
}

DWORD WINAPI __stdcall MyProc(...)
{
EnterCriticalSe ction(&cs);
__try
{
switch (NotificationTy pe)
{
...
}
}
__finally
{
LeaveCriticalSe ction(cs);
}

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

--
Chris Mullins

"thejackofa ll" <th**********@d iscussions.micr osoft.comwrote in message
news:FD******** *************** ***********@mic rosoft.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 (NotificationTy pe)
{
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!

Nov 2 '07 #2
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 NotificationTyp e is SF_NOTIFY_END_O F_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_SECTIO N cs;
string sHeader;
string sData;

DWORD WINAPI __stdcall MyProc(...)
{
EnterCriticalSe ction(&cs);

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

break;
}

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

break;
}

case SF_NOTIFY_END_O F_SESSION:
{
// release here
...
LeaveCriticalSe ction(&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 / LeaveCriticalSe ction 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 GlobalVarialIni tialization()
{
InitializeCriti calSection(&cs) ;
}

DWORD WINAPI __stdcall MyProc(...)
{
EnterCriticalSe ction(&cs);
__try
{
switch (NotificationTy pe)
{
...
}
}
__finally
{
LeaveCriticalSe ction(cs);
}

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

--
Chris Mullins

"thejackofa ll" <th**********@d iscussions.micr osoft.comwrote in message
news:FD******** *************** ***********@mic rosoft.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 (NotificationTy pe)
{
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!


Nov 2 '07 #3
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
"thejackofa ll" <th**********@d iscussions.micr osoft.comwrote in message
news:84******** *************** ***********@mic rosoft.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 NotificationTyp e is SF_NOTIFY_END_O F_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_SECTIO N cs;
string sHeader;
string sData;

DWORD WINAPI __stdcall MyProc(...)
{
EnterCriticalSe ction(&cs);

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

break;
}

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

break;
}

case SF_NOTIFY_END_O F_SESSION:
{
// release here
...
LeaveCriticalSe ction(&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.Ente r
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 / LeaveCriticalSe ction 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;
CriticalSectio n cs;

void GlobalVarialIni tialization()
{
InitializeCriti calSection(&cs) ;
}

DWORD WINAPI __stdcall MyProc(...)
{
EnterCriticalSe ction(&cs);
__try
{
switch (NotificationTy pe)
{
...
}
}
__finally
{
LeaveCriticalSe ction(cs);
}

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

--
Chris Mullins

"thejackofal l" <th**********@d iscussions.micr osoft.comwrote in message
news:FD******* *************** ************@mi crosoft.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 (NotificationTy pe)
{
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!



Nov 2 '07 #4

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

Similar topics

3
2153
by: Lars Plessmann | last post by:
Problem: I try to store data in a objects field and read it out again. Sounds easy, yeah. But its a bit tricky here.... ;-) This is the class Customer.php with some setter and getter functions to store customers data within an object. It works. Furthermore, it includes a synchronize method, which calls the individual setXXX function and takes the data from the $_POST or $_GET var.
0
595
by: dracolytch | last post by:
Good day all, Ok, I have a pretty tricky problem that I need some help with. I pass around search query information a fair amount (specifically WHERE statements). Normally, I just rawurlencode() the buggers, and pass them via the URL. I like having the where clauses in the URL, because then someone can just bookmark the URL, or send it to a friend, and I don't have to worry about a thing. If someone does a search that requires a LIKE...
4
1908
by: Bung | last post by:
Hi, I have a tricky sql statment I have to write (tricky for me) and I am stuck. I'm having trouble with the following problem. Table1 (Column a, Column b, Column c) Table2 (Column a, Column b, Column c) Table3 (Column a, Column b, Column c) Table1 contains a row of value (1, 2, 3)
25
3393
by: PyPK | last post by:
What possible tricky areas/questions could be asked in Python based Technical Interviews?
5
1626
by: Danny | last post by:
Hi there I need help with a tricky problem. I have a 2 dimensional array with qualities such as ball size, ball color, ball weight. Now I have to print out all the possible combinations of this. assume I have it stored as such i have two dimensional array ball:
13
2741
by: Steve Jorgensen | last post by:
== On Error Resume next, and Err.Number == If you want to call one of your procedures from another procedure, and check for errors afterward, you mayimagine that you should write code something like this... On Error Resuse Next MyFoo 123 lngErrNum = Err.Number On Error Goto 0
8
1879
by: pras.vaidya | last post by:
Hi , below given question was asked to me during an interview and i figured it out little tricky . It would be a great help if anyone could solve it. Code : - main() { char *s1="abcd",*s2=NULL; /* From here you call a function copy which has return type void .
2
1815
by: pruebauno | last post by:
I am currently working on a tricky problem at work. I googled around a bit, but "time intervals" did not come up with anything useful. Although I have some rough idea of how I could solve it, I still would value some input. I have information of (It has only couple dozen entries.) ServiceNum, DollarCost and a input database table in the form (several GBytes): ClientNumber (CN), BeginDate(BD), EndDate(ED),
1
1492
by: MorrganMail | last post by:
Or at least I find it tricky. :-) Assume we have three tables A, B and C. Table A contains a path and the distance for traveling that path: A (PathId, NodeId, Dist (from previous node)) 1, 1, 0 1, 2, 10 1, 3, 5
7
2269
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and matrices are sparse by design/definition in PHP. I'm doing some matrix manipulation in a Finite Element program. Translating Fortran to PHP, because hosters won't allow anything else than PHP. I wish PHP would do array and matrix stuff like Fortran...
0
8268
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8202
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8641
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.