Solved

Atomic Operations

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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

785 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