473,466 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Atomic function

What does it mean to make a function atomic?

Something I read on it wasn't very clear, but made me think that I needed to
disable interrupts for that function. Whether that's the case or not, how
would I make the following function atomic?

int Test;

int GetTestValue( void )
{
return Test;
}

Thanks in advance.

------------------------------
Brian
Nov 14 '05 #1
5 23658
Pegboy writes:
What does it mean to make a function atomic?

Something I read on it wasn't very clear, but made me think that I needed to disable interrupts for that function. Whether that's the case or not, how
would I make the following function atomic?

int Test;

int GetTestValue( void )
{
return Test;
}


It means that the entire function is completed without any intervening
actions. That is a = b + c fetches the two operands, does the add and stores
the result in a without any outside observer being able to insert anything
else into the "cracks". As far as I know this is impossible with the normal
instructions available to a C programmer, in my experience it requires a
special hardware instruction called, perhaps, "test & set" in which the
memory read and write are tightly bound to each other. So if you have
multiple processors, multiple threads, caches, and so on the code still
works. I saw some hand waving implying wonderful breakthroughs in
semaphores WRT mutexes a few days ago in one of these groups. but I wasn't
sufficiently enthused or believing to chase that rabbit,

I would expect modern microprocessors to have the needed special instruction
.. But it might be reserved for the OS to handle as part of the API or pass
through at it's discretion. Keywords mutex, semaphore, flag, "test +and
set" .....

The test & set instruction protects a body of code. Test & set a flag, do
your thing, clear the flag. Only the one instruction was special.
Nov 14 '05 #2
"Pegboy" <pe****@neb.rr.com> wrote in message news:<_S*******************@twister.rdc-kc.rr.com>...
What does it mean to make a function atomic?


If you can say with certainty that a function will execute within one
clock cycle, that function is atomic.

This is useful because something that executes within a single clock
cycle cannot be interrupted; the interrupt will either happen before
or after the function. It will never be partially executed. This is
important because in a multithreaded environment the code is
inherrently mutually exclusive (thread-safe)

---
Jared Dykstra
http://www.bork.org/~jared
Nov 14 '05 #3
"Pegboy" <pe****@neb.rr.com> wrote in message news:<_S*******************@twister.rdc-kc.rr.com>...
What does it mean to make a function atomic?

Something I read on it wasn't very clear, but made me think that I needed to
disable interrupts for that function. Whether that's the case or not, how
would I make the following function atomic?

int Test;

int GetTestValue( void )
{
return Test;
}

Thanks in advance.

------------------------------
Brian


I would suggest that you post to comp.realtime or comp.programming as
this is somewhat OT here.

p.s. The answers you were given, demonstrate why you need to post to
the right group. Neither was really correct, but then again this is a
group which talks about standard C. Hope this helps :=)
Nov 14 '05 #4
In <ba**************************@posting.google.com > dy******@hotmail.com (Jared Dykstra) writes:
"Pegboy" <pe****@neb.rr.com> wrote in message news:<_S*******************@twister.rdc-kc.rr.com>...
What does it mean to make a function atomic?


If you can say with certainty that a function will execute within one
clock cycle, that function is atomic.

This is useful because something that executes within a single clock
cycle cannot be interrupted; the interrupt will either happen before
or after the function. It will never be partially executed. This is
important because in a multithreaded environment the code is
inherrently mutually exclusive (thread-safe)


It then follows that the only atomic function is

void atomic(void) { return; }

as the machine code equivalent of the C return statement already takes
at least one clock cycle to execute ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5

"Pegboy" <pe****@neb.rr.com> wrote in message
news:_S*******************@twister.rdc-kc.rr.com...
What does it mean to make a function atomic?

Something I read on it wasn't very clear, but made me think that I needed to disable interrupts for that function. Whether that's the case or not, how
would I make the following function atomic?

int Test;

int GetTestValue( void )
{
return Test;
}

Thanks in advance.

------------------------------
Brian


Thanks all for the help.

-------------------------
Brian
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Shayan | last post by:
Is there a boolean flag that can be set atomically without needing to wrap it in a mutex? This flag will be checked constantly by multiple threads so I don't really want to deal with the overhead...
4
by: Seenu | last post by:
I'm experiencing some puzzling behaviour with some of my UDFs when declaring them as ATOMIC.. Basically I'm invoking another UDF (which uses some Java code) in one branch of a CASE statment, and...
6
by: blackstreetcat | last post by:
consider this code : int i; //gobal var Thread1: i=some value; Thread2: if (i==2) dosomething(); else dosomethingelse();
7
by: Joe HM | last post by:
Hello - I was wondering if there is a simple way of ensuring that some statements are executed as an "atomic operation". Here is what I am dealing with in a GUI ... Dim mAppDomain As...
0
by: Chris Thomasson | last post by:
Here are the pre-alpha code downloads: http://appcore.home.comcast.net/vzoom/refcount/ (both C and C++ api here...) Here is some pre-alpha documentation: ...
31
by: Michael | last post by:
Why are functions atomic? (I.e. they are not copied.) For example, I would like to make a copy of a function so I can change the default values: (2, 2) I would like the following...
2
by: Freedom fighter | last post by:
Hello, Is a singleton class the same as an atomic class? I know that a singleton class can only be instantiated once, but does that concept apply to an atomic class? Thank you.
5
by: Alex Vinokur | last post by:
void foo (int n) { std::ostringstream oss; oss << "ABCD: " << n << std::endl; std::cout << oss.str() << std::flush; } That function has been invoked in multiprocessing mode.
11
by: Jon Harrop | last post by:
Can read locks on a data structure be removed safely when updates are limited to replacing a reference? In other words, is setting a reference an atomic operation? I have been assuming that all...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.