Learn Python together: multithreading-sharing global variable issues

Source: Internet
Author: User

Multithreading-sharing global variable issues

Issues you may encounter with multithreaded development

Assuming that the two threads T1 and T2 are going to add 1 to the global variable g_num (0 by default), T1 and T2 each add 10 times to G_num, the final result of G_num should be 20.

However, due to the simultaneous operation of multiple threads, the following conditions may occur:

At the time of g_num=0, T1 obtained g_num=0. At this time the system T1 to "sleeping" state, the T2 into the "running" state, T2 also get g_num=0
T2 then adds 1 to the resulting value and assigns it to G_num, making G_num=1
Then the system T2 to "sleeping", the T1 to "running". The thread T1 and assigns it to G_num after 0 plus 1.
This led to T1 and T2 g_num plus 1, but the result is still g_num=1
Test 1

Import threadingImport Timeg_num =0DefWork1(num):Global G_numFor IIn range (num): G_num + =1 print ( "----in Work1, G_num is%d---"%g_num) def work2global g_num for i in Range (num): g_ num + = 1 print ( "----in Work2, G_num is%d---"%g_num) print ( "---thread was created before G_num is%d---"%g_num) T1 = Threading. Thread (Target=work1, args= (100,)) T1.start () t2 = Threading. Thread (TARGET=WORK2, args= (100,)) T2.start () while Len ( Threading.enumerate ())! = 1:time.sleep (1) print (" 2 threads the end result after the same global variable operation is:%s "% g_num)          

Operation Result:

is 0-------in work1, g_num is 100-------in work2, g_num is 200---2个线程对同一个全局变量操作之后的最终结果是:200

Test 2

Import threadingImport Timeg_num =0DefWork1(num):Global G_numFor IIn range (num): G_num + =1 Print ("----in Work1, G_num is%d---"%g_num) def work2(num): global g_num for I in range (num): G_num + = 1 print ("----in Work2, G_num is %d---"%g_num" print ("---thread was created before G_num is%d---"%g_num) T1 = Threading. Thread (Target=work1, args= (1000000,)) T1.start () t2 = Threading. Thread (TARGET=WORK2, args= (1000000,)) T2.start () whileLen (threading.enumerate ())! = 1:time.sleep ( 1) Print ("The end result of 2 threads after the same global variable operation is:%s"% g_num)          

Operation Result:

is 0-------in work1, g_num is 1088005-------in work2, g_num is 1286202---2个线程对同一个全局变量操作之后的最终结果是:1286202

Conclusion

If multiple threads operate on the same global variable at the same time, there is a resource contention problem that results in incorrect data

==================================================================

Note: The above course notes for the study of the teachers in the classroom study notes, if necessary to reprint, if you need full notes, please contact me privately.

Featured Python Updates My study notes every day. The above content and class notes, more details to see the original link, my public number of dry goods continue to update

SOURCE Link: Article Python developer Exchange Platform

If you have better ideas and suggestions, welcome to shoot bricks together to explore. Welcome to the public number join "Python Developer Exchange Platform"

Learn Python together: multithreading-sharing global variable issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.