C/C ++: multi-thread programming BASICS (5)

Source: Internet
Author: User

Http://blog.csdn.net/lzx_bupt/article/details/6913151

Recently, I like to hear old songs from universities. Deutschland was popular during the World Cup in Germany. It is said that it is not a theme song, but it is more popular than the theme song.

This article introduces the difficult concept of mutex lock. mutex = mutual exclusion stands for mutex =. By the way, teachers used to love the abbreviation and did not tell the full name to their classmates, can Nima have a deep understanding! The usage is as follows:

[CPP] View plaincopy
  1. # Include <iostream>
  2. # Include <pthread. h> // follow the rules
  3. Using NamespaceSTD;
  4. # Define num_threads 5
  5. IntSum = 0;// Define a global variable to allow all threads to access it. This will result in simultaneous write, and a lock mechanism is required;
  6. Pthread_mutex_t sum_mutex;
  7. Void* Say_hello (Void* ARGs)
  8. {
  9. Cout <"Hello in thread"<*((Int*) ARGs) <Endl;
  10. Pthread_mutex_lock (& sum_mutex );// Modify sum to lock the lock first. When the lock is occupied, the lock is blocked until the lock is obtained and sum is modified;
  11. Cout <"Before sum is"<Sum <"In thread"<*((Int*) ARGs) <Endl;
  12. Sum + = *((Int*) ARGs );
  13. Cout <"After sum is"<Sum <"In thread"<*((Int*) ARGs) <Endl;
  14. Pthread_mutex_unlock (& sum_mutex );// Unlock after completion and release it to other threads for use;
  15. Pthread_exit (0 );// Exit and throw a status code.
  16. }
  17. IntMain ()
  18. {
  19. Pthread_t tids [num_threads];
  20. IntIndexes [num_threads];
  21. // The next three statements are about setting thread parameters.
  22. Pthread_attr_t ATTR;
  23. Pthread_attr_init (& ATTR );
  24. Pthread_attr_setdetachstate (& ATTR, pthread_create_joinable );
  25. Pthread_mutex_init (& sum_mutex, null );// This statement is required to initialize the lock;
  26. For(IntI = 0; I <num_threads; ++ I)
  27. {
  28. Indexes [I] = I;
  29. IntRet = pthread_create (& tids [I], & ATTR, say_hello ,(Void*) & (Indexes [I]);// Modify the sum of the five processes;
  30. If(Ret! = 0)
  31. {
  32. Cout <"Pthread_create error: error_code ="<RET <Endl;
  33. }
  34. }
  35. Pthread_attr_destroy (& ATTR );// Delete parameter variables
  36. Void* Status;
  37. For(IntI = 0; I <num_threads; ++ I)
  38. {
  39. IntRet = pthread_join (tids [I], & status );
  40. If(Ret! = 0)
  41. {
  42. Cout <"Pthread_join error: error_code ="<RET <Endl;
  43. }
  44. }
  45. Cout <"Finally sum is"<Sum <Endl;
  46. Pthread_mutex_destroy (& sum_mutex );// Deregister the lock. We can see that all the built-in pthread variables correspond to the destroy function. It is estimated that the memory leakage is related;
  47. }

Convention: G ++-lpthread-O ex_mutex ex_mutex.cpp

Run:

[CPP] View plaincopy
  1. Hello inThread4
  2. Before sum is 0 inThread4
  3. After sum is 4 inThread4
  4. Hello inThread3
  5. Before sum is 4 inThread3
  6. After sum is 7 inThread3
  7. Hello inThread2
  8. Before sum is 7 inThread2
  9. After sum is 9 inThread2
  10. Hello inThread1
  11. Before sum is 9 inThread1
  12. After sum is 10 inThread1
  13. Hello inThread0
  14. Before sum is 10 inThread0
  15. After sum is 10 inThread0
  16. Finally sum is 10

It was found that thread4 was run first. It was strange that I increased progressively from 0, so the order of multithreading was chaotic and chaos was normal; as long as sum access and modification are normal, multithreading is achieved, and the running sequence cannot be used as a reference;

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.