Thread Based Parallelism - Thread Synchronization With Lock

import threading shared_resource_with_lock = 0
shared_resource_with_no_lock = 0
COUNT = 100000
shared_resource_lock = threading.Lock() ####LOCK MANAGEMENT##
def increment_with_lock():
global shared_resource_with_lock
for i in range(COUNT):
shared_resource_lock.acquire()
shared_resource_with_lock += 1
shared_resource_lock.release() def decrement_with_lock():
global shared_resource_with_lock
for i in range(COUNT):
shared_resource_lock.acquire()
shared_resource_with_lock -= 1
shared_resource_lock.release() ####NO LOCK MANAGEMENT ##
def increment_without_lock():
global shared_resource_with_no_lock
for i in range(COUNT):
shared_resource_with_no_lock += 1 def decrement_without_lock():
global shared_resource_with_no_lock
for i in range(COUNT):
shared_resource_with_no_lock -= 1 ####the Main program
if __name__ == "__main__":
t1 = threading.Thread(target=increment_with_lock)
t2 = threading.Thread(target=decrement_with_lock)
t3 = threading.Thread(target=increment_without_lock)
t4 = threading.Thread(target=decrement_without_lock)
t1.start()
t2.start()
t3.start()
t4.start()
t1.join()
t2.join()
t3.join()
t4.join()
print("the value of shared variable with lock management is %s" \
% shared_resource_with_lock)
print("the value of shared variable with race condition is %s" \
% shared_resource_with_no_lock) Output,
the value of shared variable with lock management is 0
        # 会发现 shared_resource_with_lock 恒定为 0;
        # 因为 lock 的存在, increment 的数值等于 decrement 的数值.
      the value of shared variable with race condition is -9657
        # shared_resource_with_no_lock 会为一个随机, 有时候也为 0.

Thread Based Parallelism - Thread Synchronization With Lock的更多相关文章

  1. Thread Based Parallelism - Thread Synchronization With a Condition

    Thread Based Parallelism - Thread Synchronization With a Condition from threading import Thread, Con ...

  2. Thread Based Parallelism - Thread in a Subclass

    Thread Based Parallelism - Thread in a Subclass 1 import threading import time exit_Flag = 0 class m ...

  3. Thread in depth 3:Synchronization

    Synchronization means multi threads access the same resource (data, variable ,etc) should not cause ...

  4. 【转】Native Thread for Win32 B-Threads Synchronization(通俗易懂,非常好)

    http://www.bogotobogo.com/cplusplus/multithreading_win32B.php   Synchronization Between Threads In t ...

  5. Mysql thread 与 OS thread

    测试环境信息如下: OS:Ubuntu 16.04 LTS Mysql:Mysql 5.7.18,使用docker images运行的实例 Mysql如何处理client请求 在Mysql中,连接管理 ...

  6. 源码查看Thread.interrupted()和Thread.currentThread().isInterrupted()区别

    JAVA线程状态.线程START方法源码.多线程.JAVA线程池.如何停止一个线程等多线程问题 这两个方法有点容易记混,这里就记录一下源码. Thread.interrupted()和Thread.c ...

  7. Thread.sleep( ) vs Thread.yield( )

    Thread.sleep() The current thread changes state from Running to Waiting/Blocked as shown in the diag ...

  8. Thread系列之Thread.Sleep(0)

    线程这一概念,可以理解成进程中的一个小单元.这个单元是一个独立的执行单元,但是与进程中的其他线程共享进程中的内存单元. 由于Cpu资源是有限的,所以进程中的多个线程要抢占Cpu,这也导致进程中的多个线 ...

  9. PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别

    链接:http://www.cnblogs.com/neve/articles/1863853.html 想更新个PHP的版本,PHP的windows版本已经分离出来了,见http://windows ...

随机推荐

  1. python列表的 + 、* 、in 、 not in 、 len() 、 max() 、 min()

    + 列表拼接 first_list = [1,2,3] + ['a',5] # + 将列表拼接 print(first_list) # [1, 2, 3, 'a', 5] *  列表与数字n相乘 : ...

  2. C# HttpWebRequest传递参数多种方式混合使用

    在做CS调用第三方接口的时候遇到了这样的一个问题,通过PSOTman调试需要分别在parmas.Headers.Body里面同时传递参数.在网上查询了很多资料,以此来记录一下开发脱坑历程. POSTm ...

  3. 19徐州网络赛E 线段树加离散化

    题目链接:https://nanti.jisuanke.com/t/41387 按wi的值建立权值线段树维护值为wi出现的最后位置,对于第i个人的答案,查询线段树[wi+m,max]区间的最大位置po ...

  4. FreeRTOS独立看门狗检测任务执行状态

    为了保证FreeRTOS的所有用户任务都在正常的运行,我们通过独立看门狗的形式来检测,一旦发现有某个任务长时间没有执行,看门狗就会将系统复位. 运行条件: 创建5个用户任务Task1,Task2,Ta ...

  5. 实战_Spring_Cloud

    目录 前言 开发环境 源码地址 创建工程 服务注册中心(Eureka) Eureka Server Eureka Client 注册中心高可用 小结 负载均衡(Ribbon) RestTemplate ...

  6. 关于Matplotlib中No module named 'matplotlib.finance'的解决办法

    最近在研究量化分析,需要用到matplotlib中的一个库,输入from matplotlib.finance import quotes_historical_yahoo_ohlc, candles ...

  7. Dart语言学习( 一) 为什么学习Dart?

    为什么学习Dart? Google及全球的其他开发者,使用 Dart 开发了一系列高质量. 关键的 iOS.Android 和 web 应用. Dart 非常适合移动和 web 应用的开发. 高效 D ...

  8. 演示共享布局 Demonstrating Shared Layouts 精通ASP-NET-MVC-5-弗瑞曼 Listing 5-10

  9. Spring-事务(1)

    一,注解的方式实现事务 1.Dao层 package com.atguigu.spring.tx; public interface BookShopDao { //根据书号获取书的单价 public ...

  10. php编译完php.ini加载问题-Loaded Configuration File (none)

    编译安装php7时指定了--with-config-file-path=/usr/local/php7/etc,修改了 php.ini 的配置后重启,但就是不生效. 出现Loaded Configur ...