#-*- coding:UTF-8 -*-
import thread
import time
import os

print os.path.abspath('.')

#===================测试启动多线程=====================
def timer(t_id ,s_time):
    count = 0
    while count < 10:
        print 'Thread:(%d) Time:%s\n'%(t_id, time.ctime())  
        time.sleep(s_time)
        count+=1
    print '(%d) thread wil exit ...\n'%(t_id)
    thread.exit_thread()

# thread.start_new_thread(timer, (1 , 1))
# thread.start_new_thread(timer, (2 , 2))
#===================测试多线程访问共享资源=====================
mNum = 0
# def addTest(tname):
#     global mNum
#     for index in range(1000000):
#         mNum += 1
#     print '(%s) thread exit ...'%(tname)
#     

#===================测试多线程访问共享资源加锁===================
mLock = thread.allocate_lock()

def addTest(tname):
    global mNum
    for index in range(1000000):
        mLock.acquire()
        mNum += 1
        mLock.release()
    print '(%s) thread exit ...'%(tname)
  
thread.start_new_thread(addTest, ('thread_1',))
thread.start_new_thread(addTest, ('thread_2',))
thread.start_new_thread(addTest, ('thread_3',))
thread.start_new_thread(addTest, ('thread_4',))  
#========================================
#========================================
#========================================
#========================================
#========================================
time.sleep(70)
print "Main thread will exit ... <<< %d"%mNum