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

Source: Internet
Author: User

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

On the basis of thinking, what if the function called by the thread is in a class? The answer is to write the function as a static member function. The following mode conforms to the C ++ writing mode:

[CPP] View plaincopy
    1. # Include <iostream>
    2. # Include <pthread. h>
    3. Using NamespaceSTD;
    4. # Define num_threads 5
    5. ClassHello
    6. {
    7. Public:
    8. Static Void* Say_hello (Void* ARGs)// There is no difference except for the static keyword;
    9. {
    10. Cout <"Hello ..."<Endl;
    11. }
    12. };
    13. IntMain ()
    14. {
    15. Pthread_t tids [num_threads];
    16. For(IntI = 0; I <num_threads; ++ I)
    17. {
    18. IntRet = pthread_create (& tids [I], null, hello: say_hello, null );
    19. If(Ret! = 0)
    20. {
    21. Cout <"Pthread_create error: error_code ="<RET <Endl;
    22. }
    23. }
    24. Pthread_exit (null );
    25. }

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.