|
Perl 6 FAQ - Concurrency
This FAQ is part of the Programmer's Heaven Perl 6 FAQ. It covers the plans for concurrency support in Perl 6.What will threading look like?
Perl 6 provides async blocks. Code placed in such a block will be executed in a separate thread to the main program, with execution continuing with the instruction following the block.async {
# Code here runs in another thread.
}
# Code here after the block is back in the parent thread.
You can write multiple async blocks one after the other to spawn multiple threads, or even use a loop to spawn many threads doing the same thing. There may be nicer syntax for that some day - the threading specification is not finalized yet.If you need control over the thread that was spawned, async returns an object that allows you to do that.
my $thread = async {
# Do stuff.
}
# It numifies to the thread ID.
say "I spawned thread ID " ~ +$thread;
# Wait for the thread to finish.
$thread.join();What is Software Transactional Memory (STM)?
A transaction is a sequence of operations that are either completed atomically or that don't complete and have no effect on the state of the program. Database transactions are heavyweight and carry a load of properties relating to durability (persistence of data once a transaction has been completed) too. STM, on the other hand, is a very lightweight transaction mechanism and, in a good implementation, cache-sensitive so as to achieve high performance and scalability.STM tracks what variables you read from and write to. Changes are not made to the real variables, but instead are stored in a transaction log. Outside of the current transaction, no other parts of the program can see the changes. At the end of the transaction, a commit is attempted. If no other transaction has committed a change to a variable you read or wrote to, your changes are made and the transaction is successful. Otherwise, it fails and has to be repeated again. This has the nice property that at least some thread in the system has made progress, though.
Should an error occur during your transaction, the transaction is aborted. The transaction log is discarded. Since no changes were made to the real data, this is all that is required to roll back the transaction. Because transactions need to be able to be rolled back, they cannot have any side-effects in the "outside world", meaning I/O is not allowed to take place.
STM provides concurrency control without burdening the programmer with having to worry about taking locks. It allows for a more declarative style of concurrent programming, where you just say that a block of code should have transactional properties and its up to the compiler/runtime to make that happen.
What will using STM look like in Perl 6?
The current plan, and prototype implementation, is that Perl 6 will expose STM using the atomic keyword. Code that should have transactional semantics will go in an atomic block.atomic {
$account_a.debit(100);
$account_b.credit(100);
}
Here we call methods; the transactional semantics will extend to those too. Attempting to do any I/O inside a transaction will result in a fatal error.How is Perl 6 providing support for data parallelism?
When you use hyper operators, cross operators or reduction operators, you are telling the compiler that there are no data dependencies between the elements of the lists that you are operating on. This means that it is free to perform these operations in parallel. This probably will not be the default; you will likely have to use a pragma to specify that you want parallel execution over different sections of the array.Back
What's next?
Join our Perl 6 Newsletter
Visit our Perl Resources * Perl 6 Forum * Perl, PHP & Python zone * Perl Programming Forum * Beginners Guide to Perl * Regex tutorial * 20 Perl Tips And Tricks
|
|
riya
From India (Report as abusive) |
"Very Useful" this faqs made to know about many things unknown and it helped me a lot |
| View all Rate and comment this article |
Sponsored links
SFTP components for .NET
Add complete SSH and SFTP support to your .NET framework application
Add complete SSH and SFTP support to your .NET framework application
Virtual File System SDK
Create your own file systems in Windows and .NET applications
Create your own file systems in Windows and .NET applications
PureCM Software Configuration Management
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!
CSTSOFT Instrumentation .NET & ActiveX Components
A collection of 13 instrumentation .NET/ActiveX/VCL components including Gauge,Knob,LED,Trend etc.
A collection of 13 instrumentation .NET/ActiveX/VCL components including Gauge,Knob,LED,Trend etc.
Software Localization Tool Sisulizer
Localize DotNet, C++ Builder, Delphi, C/C++, Visual Basic & Java apps & html help. Try Sisulizer now
Localize DotNet, C++ Builder, Delphi, C/C++, Visual Basic & Java apps & html help. Try Sisulizer now