Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Atomic Operations

Posted on 1997-06-07
5
902 Views
Last Modified: 2010-08-05
Hi there:

Does anyone know if there is possibility of doing Atomic Operations in
C++?
****** NEW Lines:
Particularly I am interested to have the solution on Windows NT Platform.
Example:
I want to COMMIT to a database (operation 1) and rename a file (operation 2) in one operation. Operation 2 is to be done after Operation 1 but I don't want to do Operation 1 without making sure that Operation 2 is also done!!

By Atomic I mean a block of code that either gets executed entirely
without interruption by the CPU or it does not get executed at all.

I might be wrong but I think Synchronize keyword in Java does that.
0
Comment
Question by:farshid
5 Comments
 
LVL 4

Expert Comment

by:md041797
ID: 1163862
On what platform?
There are no C++ language constructs, so you need to know the operating system.
0
 
LVL 3

Expert Comment

by:byang
ID: 1163863
Exactly what do you need to do ?
0
 

Expert Comment

by:heaveyrl
ID: 1163864
In MS Visual C++ (4.2CAB) use help/query type in the keyword
"synchronization" then read all the text up on "Application and Thread Support Classes" and "MultiThreading: How to Use Synchronization Classes".

At the kernel is Mutex, which you'll have to create, manage and observe to give your go / no go functionality.


0
 

Author Comment

by:farshid
ID: 1163865
Edited text of question
0
 

Accepted Solution

by:
aviad earned 100 total points
ID: 1163866
There are two ways to handle pieces of code that need to be protected through mutual exclusion from other pieces of code.
1. Mutex Semaphores
2. Using "Critical Sections"

From your question I think the latter is more suitable for you. And there are two ways to use critical sections in windows NT.
1. By using the functions InitializeCriticalSection(), EnterCriticalSection(), LeaveCriticalSection()
2. By using the CCriticalSection and the CSingleLock or CMultiLock classes.

The first method is simple and straightforward, you initialize a so-called "critical-section" object somewhere in the initialization of your program, this object has no meaning in itself, but will be used in subsequent calls to EnterCriticalSection() and LeaveCriticalSection().

The second method is a bit more complicated but is more object-oriented. Using the second method, the class containing the critical section should include a data member of type CCriticalSection. Then in order to handle the controlled section, you should define a local variable of type CSingleLock (or CMultiLock if you want to wait on more than one CCriticalSection object). An example will show it best:

Assume class "List" has critical sections of code dealing with insertions and removals of items:

class List {
private:
    CCriticalSection Sync;
public:
    void Add() {
        CSingleLock SLock(&Sync);
        SLock.Lock();

              //{ critical section code }

        SLock.Unlock()
    }
...
};

Hope this helps.
Aviad.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

766 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