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

Source: Internet
Author: User

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

The thread will be created. How do I pass in parameters when the thread calls a function? It should be as follows:

[CPP] View plaincopy
  1. # Include <iostream>
  2. # Include <pthread. h>
  3. Using NamespaceSTD;
  4. # Define num_threads 5
  5. Void* Say_hello (Void* ARGs)
  6. {
  7. IntI = *((Int*) ARGs );// Forcibly convert the input parameter type, from a non-type pointer to an integer pointer, and then read it;
  8. Cout <"Hello in"<I <Endl;
  9. }
  10. IntMain ()
  11. {
  12. Pthread_t tids [num_threads];
  13. Cout <"Hello in Main ..."<Endl;
  14. For(IntI = 0; I <num_threads; ++ I)
  15. {
  16. IntRet = pthread_create (& tids [I], null, say_hello ,(Void*) & I );// It must be forcibly converted to the void * type when passed in, that is, no type pointer
  17. Cout <"Current pthread id ="<Tids [I] <Endl;// Use the tids array to print the ID of the created process;
  18. If(Ret! = 0)
  19. {
  20. Cout <"Pthread_create error: error_code ="<RET <Endl;
  21. }
  22. }
  23. Pthread_exit (null );
  24. }

The result is as follows:

[Plain] View plaincopy
    1. Current pthread id = 139671233451792
    2. Current pthread id = 139671222961936
    3. Current pthread id = 139671212472080
    4. Current pthread id = 139671201982224
    5. Current pthread id = 139671191492368
    6. Hello, in 4196496
    7. Hello, in 4196496
    8. Hello, in 4196496
    9. Hello, in 4196496
    10. Hello, in 4196496

Have you found any problems? Yes, the I value does not output the expected results. This is due to multithreading. When the main process has not been assigned a value for I, the thread has started to run !~

Then the followingCodeIs correct:

[CPP] View plaincopy
  1. # Include <iostream>
  2. # Include <pthread. h>
  3. Using NamespaceSTD;
  4. # Define num_threads 5
  5. Void* Say_hello (Void* ARGs)
  6. {
  7. Cout <"Hello in thread"<*((Int*) ARGs) <Endl;
  8. }
  9. IntMain ()
  10. {
  11. Pthread_t tids [num_threads];
  12. IntIndexes [num_threads];// Use an array to save the I value.
  13. For(IntI = 0; I <num_threads; ++ I)
  14. {
  15. Indexes [I] = I;// Save the I value first, so the problem will not occur in the calling thread.
  16. IntRet = pthread_create (& tids [I], null, say_hello ,(Void*) & (Indexes [I]);
  17. If(Ret! = 0)
  18. {
  19. Cout <"Pthread_create error: error_code ="<RET <Endl;
  20. }
  21. }
  22. For(IntI = 0; I <num_threads; ++ I)
  23. Pthread_join (tids [I], null );
  24. }

Compile and run: (SourceProgramRemove the nonsense of printing thread ID)

[Plain] View plaincopy
      1. [CPP @ node2 pthread] $./ex_create_args_ OK
      2. Hello in thread 3
      3. Hello in thread 4
      4. Hello in thread 2
      5. Hello in thread 1
      6. Hello in Thread 0

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.