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

Source: Internet
Author: User

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

The previous examples show whether the attribute parameters are not mentioned when few threads are created. See the example below:

[CPP] View plaincopy
  1. # Include <iostream>
  2. # Include <pthread. h>
  3. # Include <iostream>
  4. # Include <pthread. h>
  5. Using NamespaceSTD;
  6. # Define num_threads 5
  7. Void* Say_hello (Void* ARGs)
  8. {
  9. Cout <"Hello in thread"<*((Int*) ARGs) <Endl;
  10. IntStatus = 10 + *((Int*) ARGs );// Add the parameter to 10
  11. Pthread_exit ((Void*) Status );// Because the joinable parameter is provided during thread creation, the exit information can be added here: status for the masterProgramExtract the end information of the thread;
  12. }
  13. IntMain ()
  14. {
  15. Pthread_t tids [num_threads];
  16. IntIndexes [num_threads];
  17. Pthread_attr_t ATTR;// To add parameters during creation, declare
  18. Pthread_attr_init (& ATTR );// Reinitialize
  19. Pthread_attr_setdetachstate (& ATTR, pthread_create_joinable );// After the declaration and initialization, the third step is to set the thread attribute parameter you want to specify. This parameter indicates that the thread can be joined, the join function indicates that the main program can wait for the thread to end before doing something. The main program and the thread synchronization function are implemented. This deep understanding must be illustrated to explain it. See other materials.
  20. For(IntI = 0; I <num_threads; ++ I)
  21. {
  22. Indexes [I] = I;
  23. IntRet = pthread_create (& tids [I], & ATTR, say_hello ,(Void*) & (Indexes [I]);// The four parameters are complete. More configuration information is required;
  24. If(Ret! = 0)
  25. {
  26. Cout <"Pthread_create error: error_code ="<RET <Endl;
  27. }
  28. }
  29. Pthread_attr_destroy (& ATTR );// The parameter can be destroyed after it is used up. It must be destroyed to prevent memory leakage;
  30. Void* Status;
  31. For(IntI = 0; I <num_threads; ++ I)
  32. {
  33. IntRet = pthread_join (tids [I], & status );// A thread is created before. Here, the main program wants to join each thread and obtain the exit information of each thread;
  34. If(Ret! = 0)
  35. {
  36. Cout <"Pthread_join error: error_code ="<RET <Endl;
  37. }
  38. Else
  39. {
  40. Cout <"Pthread_join get status :"<(Long) Status <Endl;
  41. }
  42. }
  43. }

Compile and run g ++-lpthread-O ex_join ex_join.cpp

Result:

[Plain] View plaincopy
    1. Hello in thread 4
    2. Hello in thread 3
    3. Hello in thread 2
    4. Hello in thread 1
    5. Hello in Thread 0
    6. Pthread_join get status: 10
    7. Pthread_join get status: 11
    8. Pthread_join get status: 12
    9. Pthread_join get status: 13
    10. Pthread_join get status: 14

Let's take a look at the join function.

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.