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. 解决el-tree lazy懒加载时,连续勾选前两个子节点后第二次进入默认选中时,将父节点也勾选的问题

    在用到el-tree的懒加载和默认勾选功能时,若第一次勾选前几个连续节点,第二次进入默认勾选时,由于el-tree子节点尚未完全加载(只加载出来前几个),默认勾选已经开始(已加载出来的子节点被默认勾选 ...

  2. C语言编译成dll

    首先c语言在开始要加上 #ifdef __cplusplus extern "C" { #endif …被导出的方法名称 #ifdef __cplusplus } #endif 不 ...

  3. html+css 知识点总结 day1(01-08)

    01  初步认识浏览器 02 浏览器内核 IE   内核:Trident,                 win10 Edge  内核:EdgeHTML Firefox(火狐浏览器)   内核:Ge ...

  4. if(a)是什么意思

    if(a)等价于 if(a!=0) if(!a)等价于 if(a==0)

  5. nginx之文件配置

    nginx配置规则 nginx由受配置文件中指定的指令控制的模块组成 伪指令分为简单伪指令和块伪指令 简单的指令由名称和参数组成,这些名称和参数之间用空格分隔,并以分号(;)结尾 块指令的结构 与 简 ...

  6. nmap详解之原理与用法

    前言 nmap是一款开源免费的网络发现(Network Discovery)和安全审计(Security Auditing)工具.软件名字Nmap是Network Mapper的简称.Nmap最初是由 ...

  7. Centos 7 下部署集群式阿波罗

    apollo工作原理 用户通过浏览器登录Portal管理界面 >> 通过Admin server对配置进行修改 >> 应用程序主动向config server配置注意:Port ...

  8. JDBC的学习笔记-手动实现

    JDBC是SUN公司提供的一套用于数据库操作的接口,Java程序员只需要面向这套接口编程即可.不同的数据库厂商,需要针对这套接口,提供不同实现. 使用JDBC的好处:1.程序员不需要关注不同数据库的细 ...

  9. MySQL5.7 中的query_cache_size

    摘自:http://jackyrong.iteye.com/blog/2173523 1 原理    MySQL查询缓存保存查询返回的完整结果.当查询命中该缓存,会立刻返回结果,跳过了解析,优化和执行 ...

  10. Kafka源码工程examples项目配置log4j

    examples项目启动想知道有哪些错误,通过日志了解代码执行逻辑,但是启动SimpleConsumerDemo了报错如下: log4j.proproties也配置了 log4j.proproties ...