thread、threading

互斥锁、嵌套互斥锁、条件变量、Event、local

local:全局定义,线程局部变量。其它线程环境下修改不影响其它线程

thread 

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import thread, time

g_loack = thread.allocate_lock()
g_count = 0

def func1():
    global g_count, g_loack

    #锁操作
    g_loack.acquire()
    val = g_count
    val += 1
    g_count = val
    g_loack.release()

    print 'done func1'

def func2():
    global g_count, g_loack

    #锁操作
    g_loack.acquire()
    val = g_count
    val += 1
    g_count = val
    g_loack.release()

    print 'done func2'

def add_thread():
    return thread.start_new_thread(func1), thread.start_new_thread(func2)

if __name__ == '__main__':
    thread_list = []
    for n in range(10):
        thread_list.extend(add_thread())

    time.sleep(10)
    print g_count



 

threading

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import time
import threading

g_count = 0
g_mutex_lock = threading.Lock()  #全局互斥锁
g_mutex_rlock = threading.RLock() #可重入锁,防止反复加锁造成死锁
g_condigtion = threading.Condition()  #条件变量

class TheadTest(threading.Thread):
    def __init__(self, **kwargs):
        super(TheadTest, self).__init__(**kwargs)
        self.event = threading.Event()

    def run(self):
        """
        工作函数,父类调用
        """

    def mutex_lock_test(self):
        """
        数据互斥,加锁解锁
        """
        global g_count, g_mutex_lock

        g_mutex_lock.acquire()
        #g_mutex_lock.acquire()  #重复加锁造成死锁,可使用RLock
        count = g_count
        count += 1
        g_count = count
        #g_mutex_lock.release()
        g_mutex_lock.release()

    def mutex_rlock_test(self):
        """
        重入互斥锁
        可反复加锁防止死锁
        """
        global g_count, g_mutex_rlock

        g_mutex_rlock.acquire()
        g_mutex_rlock.acquire()
        count = g_count
        count += 1
        g_count = count
        g_mutex_rlock.release()
        g_mutex_rlock.release()

    def condigtion_wait(self):
        """
        条件变量
        """
        global g_count, g_condigtion

        #加锁
        g_condigtion.acquire()
        #判断条件
        while g_count < 10:
            g_condigtion.wait()
        #循环条件不成立break
        g_condigtion.release()

    def condigtion_notify(self):
        global g_condigtion, g_count

        g_condigtion.acquire()
        g_count = 10
        g_condigtion.notify()  #激活条件变量中加锁等待的线程
        g_condigtion.release()

    def event_wait(self):
        self.event.wait()
        print 'event set'

    def event_set(self):
        self.event.set()


class TheadTestExt(TheadTest):
    def run(self):
        print 'thead name:%s' % self.name
        #self.mutex_lock_test()
        #self.mutex_rlock_test()
        #self.condigtion_wait()
        self.event_wait()


def thead_exec(cls, count=1):
    theads = [cls(name='thead num %s' % count) for count in range(count)]
    for thead in theads:
        thead.start()
    return theads

if __name__ == '__main__':

    #条件变量测试
    theads = thead_exec(TheadTestExt)
    time.sleep(1)
    theads[0].event_set()
    for thead in theads:
        thead.join()
    pass



 

转载于:https://my.oschina.net/u/2542126/blog/760052

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值