Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Atomic Operations

Posted on 1997-06-07
5
Medium Priority
?
907 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
[X]
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
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 300 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
Suggested Courses

721 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