Solved

Atomic Operations

Posted on 1997-06-07
5
888 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

760 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

19 Experts available now in Live!

Get 1:1 Help Now