Why do we need STM (software transactional memory)

Source: Internet
Author: User

Update: 2013-8-20

From the pypy blog point of view, they have already implemented the STM version of pypy: http://morepypy.blogspot.com/2013/08/update-on-assist.html. There are still many questions.

Recently, I saw a blog by a Chinese cool-man: http://www.cnblogs.com/coryxie/, and many of them are locked.Algorithm, The stuff of STM.

I realized that both the software transaction memory and the hardware transaction memory had been implemented ..

---------------------------------------------------------------------------------

Original article: http://morepypy.blogspot.com/2011/08/we-need-software-transactional-memory.html

This is a blog written by pypy developers, which mentions the implementation of multiple threads such as Python and Java, and an interesting stuff: STM (software transactional memory ).

Briefly record some experiences and ideas (which may be incorrect :)).

Most scripting languages do not have a multi-thread mechanism (coroutine is supported, but this is actually executed in a single thread ). Why?

Why does Java support multithreading in parallel? Why are we executing the wrong non-synchronous multithreading?CodeDoes JVM crash?

We all know that, for example, C/C ++, if the execution of wrong non-synchronous multi-thread code,ProgramIt is easy to crash. Because of pointers or something, multiple threads are messing up together, and the memory will be messy immediately, and the program will naturally crash.

The virtual machine running the program must provide such a guarantee:
When a thread is reading or writing an object, another thread is writing this object,
The program will only read the old object or the new object, but will not read the data half written to another thread ).
In addition, the virtual machine does not crash (even if the user writes the wrong code ).

First, consider JAVA Implementation, Because Java does not have a built-in list and dict, for Java. util. hashmap does not guarantee the security of multithreading (although an exception may be thrown, It is thrown in the hashmap code, rather than its own mechanism ). Therefore, in Java, you only need to resolve object references. On 32-bit machines, the CPU ensures 4-byte read/write atomicity, so JAVA does not need to be locked. If you modify an object, you can directly modify the referenced one.

In python, Python supports list and dict native, so it must provide atomic operations on the two (of course there are other things that need to support atomic operations ). For example, if the get and remove operations of dict are not atomic, dict will be damaged in the case of multiple threads.

Let's take a look at how the python parser is implemented:

Cpython uses Gil (Global interpreter lock) to lock each bytecode, which is about 100 (many people misunderstand cpython that it can only be executed in a single thread, in fact, it can run in multiple threads ).
Jpython uses Java-level locks to lock every required operation. Because Java optimization is awesome, the efficiency is acceptable (why not use C to implement this, or why cpython does not, because it has to do a lot of work, and may bring many subtle bugs ).
Pypy is similar to cpython.

What is STM?

STM is a bit like a database transaction, but STM is for program execution, probably like this process:

When the program starts to execute "bytecode", it first starts a transaction and uses a threadlocal stuff to record the log in the transaction.

Records all objects read and records of modified objects

During the above process, "probe" has not been modified, for example, check whether the last modification time of an object is consistent with that at the beginning of the transaction.

If no modification is found at the end of "bytecode", the modified memory will be "commit". If there is any modification, "rollback" will be performed ".

To put it another way, when cpython begins to obtain the Server Load balancer, it starts the transaction and commits the transaction when it releases the Server Load balancer.

When cpython calls the system call (release the template), it ends the transaction. When the system call ends (obtain the template), it starts the transaction.

It is really interesting to say that STM. However, pypy has not yet been implemented and I don't know how efficient it will be.

However, I personally feel that even if this is done, I am afraid the efficiency is hard to keep up. Imagine a statistical program with hundreds of threads sharing a result variable count. In this way, the Code executed every time is "rollback", which is more tragic than a single thread.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.