About using "Critical Section"

Posted on 1997-07-03
Medium Priority
Last Modified: 2010-08-05
   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.WriteString( strOut );
      CATCH( CFileException, e )
#if defined ( _DEBUG )
      TRACE( CString( "Could not write to file " ));
Question by:jpchen
  • 2

Accepted Solution

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

    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...)

Expert Comment

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


Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

587 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