473,568 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ 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 23671
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******* ************@tw ister.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******* ************@tw ister.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.programmin g 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******@hotmai l.com (Jared Dykstra) writes:
"Pegboy" <pe****@neb.rr. com> wrote in message news:<_S******* ************@tw ister.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******** ***********@twi ster.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
3325
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 of mutexes or semaphores. Thanks. Shayan
4
1829
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 if that UDF is declared as ATOMIC then both branches get executed! I've put together some simple SQL (see below) which shows this. When I invoke...
6
6240
by: blackstreetcat | last post by:
consider this code : int i; //gobal var Thread1: i=some value; Thread2: if (i==2) dosomething(); else dosomethingelse();
7
3584
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 AppDomain The following sets the mAppDomain in a function ... mAppDomain = AppDomain.CreateDomain(lFullPathAssembly)
0
1359
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: http://appcore.home.comcast.net/vzoom/refcount/doc/
31
1760
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 behaviour: (1,2)
2
2929
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
2452
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
6316
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 writes of <=1 word of data are atomic. Is this actually documented anywhere? -- Dr Jon D Harrop, Flying Frog Consultancy...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6277
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.