thread、threading
博客专区 > cics 的博客 > 博客详情
thread、threading
cics 发表于2年前
thread、threading
  • 发表于 2年前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】新注册用户域名抢购1元起>>>   

互斥锁、嵌套互斥锁、条件变量、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



 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 14
码字总数 5770
×
cics
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: