Solved

About using "Critical Section"

Posted on 1997-07-03
2
221 Views
Last Modified: 2010-08-05
Hi,
   I want to block the system during my function using Critical Section.
   How can I use it to my function?
====================================================
void CLogFile::WriteToLog( const CString& strMsg )
{      TRY
      {
       oOut.Open(FileName.. );
       oOut.SeekToEnd();
       oOut.WriteString( strOut );
       oOut.Close();
      }
      CATCH( CFileException, e )
      {
#if defined ( _DEBUG )
      TRACE( CString( "Could not write to file " ));
#endif
      }
      END_CATCH
}
=========================================================
Thanks!!
         
0
Comment
Question by:jpchen
  • 2
2 Comments
 
LVL 2

Accepted Solution

by:
sprinkmeier earned 50 total points
ID: 1164275
If you create an instance if the ICritSect class, then you have created a critical section, ie.

foo()
{
  .....code....
  {
    ICritSect crit;
    // this code runns 'critical'
    .....
  }
  // end ccritical section
  ...
}


However, I'm not sure this is what you want. Critical secitons only disable other threads in a given progcess, I'm not sure of how to disable "the system" (I assume you mean the kernel).
You could try boosting your priority to 'slow-down' the rest of the system (yuck!), or a shared mutex sem to stop cooperating processes.
BEWARE!! MUTEX semaphores and critical sections do not mix well, all but the most trivial examples are prone to all sorts of deadlocks.

(tell me if you need the C API calls for the above. bit rusty on those...)
0
 
LVL 2

Expert Comment

by:sprinkmeier
ID: 1164276
Me again...
You did not specify a platform/OS, and I assumed OS/2 (cos that's what I know).

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now