官术网_书友最值得收藏!

Software transactional memory

The idea of database transactions is also based around concurrent reads and writes. A transaction embodies an atomic operation, which means that either all or none of the steps in the operation are completed. If all the operations are completed, the transaction succeeds; otherwise, the transaction aborts. The software transactional memory is a concurrency control mechanism on similar lines. It, again, is a different paradigm, an alternative to lock-based synchronization.

Just like a database transaction, a thread makes modifications and then tries to commit the changes. Of course, if some other transaction wins, we roll back and retry. If there is an error, the transaction aborts and we retry again.

This scheme of things is called optimistic locking, wherein we don't care about other possible concurrent transactions. We just make changes and hope the commit succeeds. If it fails, we keep trying until it eventually succeeds.      

What are the benefits? We get increased concurrency, as there is no explicit locking, and all threads keep progressing; only in the case of a conflict will they retry.      

STM simplifies our understanding of multithreaded programs. This, in turn, helps make programs more maintainable. Each transaction can be expressed as a single-threaded computation, as shown in the following diagram. We don't have to worry about locking at all:

Composability is a big theme: lock-based programs do not compose. You cannot take two atomic operations and create one more atomic operation out of them. You need to specifically program a critical section around these. STM, on the other hand, can wrap these two operations inside a transaction block, as shown in the preceding diagram. 

主站蜘蛛池模板: 安顺市| 长武县| 河源市| 健康| 福安市| 九江县| 讷河市| 大兴区| 永胜县| 岳池县| 理塘县| 米脂县| 新化县| 大悟县| 稷山县| 紫阳县| 潜山县| 萨迦县| 聊城市| 康保县| 青神县| 新巴尔虎右旗| 尉氏县| 林周县| 登封市| 雅安市| 毕节市| 麦盖提县| 东方市| 宜兰市| 石景山区| 商河县| 武胜县| 海原县| 扎兰屯市| 察隅县| 华亭县| 平乡县| 施秉县| 陆川县| 盐亭县|