Linux C multi-thread example

Source: Internet
Author: User

Create two threads to increase the number of threads.
Maybe this instance has no practical value, but with a slight change, we can use it elsewhere.

  1. # Include <pthread. h>
  2. # Include <stdio. h>
  3. # Include <sys/time. h>
  4. # Include <string. h>
  5. # Include <unistd. h>
  6. # Define max 10
  7. Pthread_tThread[2];
  8. Pthread_mutex_t mut;
  9. IntNumber = 0, I;
  10. Void* Thread1 (Void*)
  11. {
  12. Printf ("Thread1: I'm thread 1 \ n");
  13. For(I = 0; I <Max; I ++)
  14. {
  15. Printf ("Thread1: Number = % d \ n", Number );
  16. Pthread_mutex_lock (& MUT );
  17. Number ++;
  18. Pthread_mutex_unlock (& MUT );
  19. Sleep (2 );// The pthread_delay_np function can be used to sleep a thread for 2 seconds.
  20. }
  21. Printf ("Thread1: The main function is waiting for me? \ N");
  22. Pthread_exit (null );
  23. }
  24. Void* Thread2 (Void*)
  25. {
  26. Printf ("Thread2: I'm thread 2 \ n");
  27. For(I = 0; I <Max; I ++)
  28. {
  29. Printf ("Thread2: Number = % d \ n", Number );
  30. Pthread_mutex_lock (& MUT );
  31. Number ++;
  32. Pthread_mutex_unlock (& MUT );
  33. Sleep (3 );
  34. }
  35. Printf ("Thread2: The main function is waiting for me? \ N");
  36. Pthread_exit (null );
  37. }
  38. VoidThread_create (Void)
  39. {
  40. IntTemp;
  41. Memset (&Thread, 0,Sizeof(Thread));// Comment1
  42. /* Create thread */
  43. If(Temp = pthread_create (&Thread[0], null, thread1, null ))! = 0)// Comment2
  44. Printf ("Thread 1 fail to create! \ N");
  45. Else
  46. Printf ("Thread 1 created! \ N");
  47. If(Temp = pthread_create (&Thread[1], null, thread2, null ))! = 0)// Comment3
  48. Printf ("Thread 2 fail to create! ");
  49. Else
  50. Printf ("Thread 2 created! \ N");
  51. }
  52. VoidThread_wait (Void)
  53. {
  54. /* Wait thread end */
  55. If(Thread[0]! = 0 ){// Comment4
  56. Pthread_join (Thread[0], null );
  57. Printf ("Thread 1 completed \ n");
  58. }
  59. If(Thread[1]! = 0 ){// Comment5
  60. Pthread_join (Thread[1], null );
  61. Printf ("Thread 2 completed. \ n");
  62. }
  63. }
  64. IntMain ()
  65. {
  66. /* Init mutex */
  67. Pthread_mutex_init (& Mut, null );
  68. Printf ("Main function, creating thread... \ n");
  69. Thread_create ();
  70. Printf ("Main function, waiting for thread end... \ n");
  71. Thread_wait ();
  72. Return0;
  73. }

 

 

I am a main function. I am creating a thread, haha
thread 1 is created
thread 2 is created
I am the main function. I am waiting for the thread to complete the task. Haha
thread1: i'm thread 1
thread1: Number = 0
thread2: I'm thread 2
thread2: Number = 1
thread1: number = 2
thread2: Number = 3
thread1: Number = 4
thread2: Number = 5
thread1: Number = 6
thread1: number = 7
thread2: Number = 8
thread1: Number = 9
thread2: Number = 10
thread1: is the main function waiting for me to complete the task?
thread 1 has ended
thread2: is the main function waiting for me to complete the task?
thread 2 has ended

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.