how to make atomic instruction

in multithreading if many threads change the value of a global variable,(instruction is in execute procedure) like this:

 value:=value+1;

for sure this one must be an atomic instruction, so what should I add to my code to make this instruction atomic?

Answers


In modern Delphi you should use the intrinsic function AtomicIncrement. There are a whole family of such functions in the RTL that provide atomic operations. For instance atomic exchange, atomic compare and swap and so on.

For your code you would write:

AtomicIncrement(value);

The benefit of using intrinsic functions is that the compiler inlines the asm instructions at the call site.

In older versions of Delphi you should use InterlockedIncrement. Again, there are a family of similarly named functions.


Need Your Help

Why does tracking branch create a new remote branch?

git version-control github

I had a git repository with a remote branch origin/master.

Rails 3 Rake Clone Database for Testing Environment

ruby-on-rails testing ruby-on-rails-3 migration

Is there a rake command in Rails 3 to clone my development database data? I noticed rake db:test:prepare and rake db:test:clone are mentioned throughout various blogs, but running them seems to do

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.