TCP/IP network programming multi-thread server implementation (1)

Source: Internet
Author: User

TCP/IP network programming multi-thread server implementation (1)

Basic thread concepts

We have mentioned the multi-process server before, but we know that it has a high overhead, So we introduce the thread. we can regard it as a lightweight process. It has the following advantages over processes:

Thread creation and context switching are less costly and faster.

Special techniques are not required when data is exchanged between threads.

Process: in the operating system, a separate execution flow unit is formed.

Thread: the unit in which the process constitutes a separate execution flow.

Their inclusion relationships are: Operating System> process> thread. The specific difference between processes and threads is that each process has an independent and complete memory space, including the global data zone, heap zone, and stack zone, the overhead of the multi-process server is high because the data zone, heap zone, and stack zone memory are all copied to distinguish different function flows in the stack zone. Multithreading is much more efficient. It only separates the stack zone, and the data zone in the process is shared. An example of the memory structure is shown below:

 

Thread creation and running

The thread has a separate execution flow. Therefore, you must define the thread entry function separately and request the operating system to execute this function in a separate execution flow. The function to complete this function is as follows:

 
 
  1. # Include
  2.  
  3. Int pthread_create (
  4.  
  5. Pthread_t * restrict thread, // save thread ID
  6.  
  7. Const pthread_attr_t * restrict attr, // thread attribute, NULL default attribute
  8.  
  9. Void * (* start_routine) (void *), // thread entry function, function pointer
  10.  
  11. Void * restrict arg // parameters passed to the entry function
  12.  
  13. );

Instance code:

 
 
  1. #include 
  2.  
  3. #include 
  4.  
  5. #include 
  6.  
  7. void * thread_main(void *arg); 
  8.  
  9. int main(int argc, char *argv[]) 
  10.  
  11.  
  12. pthread_t t_id; 
  13.  
  14. int thread_param = 5; 
  15.  
  16. if (pthread_create(&t_id, NULL, thread_main, (void *)&thread_param) != 0) 
  17.  
  18.  
  19. puts("pthread_create() error"); 
  20.  
  21. return -1; 
  22.  
  23.  
  24. sleep(10); 
  25.  
  26. puts("end of main"); 
  27.  
  28. return 0; 
  29.  
  30.  
  31. void * thread_main(void *arg) 
  32.  
  33.  
  34. int i; 
  35.  
  36. int cnt =* ((int *)arg); 
  37.  
  38. for (i = 0; i < cnt; i++) 
  39.  
  40.  
  41. sleep(1); 
  42.  
  43. puts("running thread"); 
  44.  
  45.  
  46. return NULL; 
  47.  
  48. }

 

The above example uses sleep delay to control thread execution. If the main thread does not delay the execution, it is returned to return 0; when the process ends, the corresponding thread will be destroyed. Obviously, it is unreasonable to use sleep to control the thread execution stream. Let's look at a better delay function. The process (or thread) that calls this function will enter the waiting state, until the thread with the first parameter ID is terminated. In addition, the return value of the entry function of the thread can be obtained.

 
 
  1. # Include
  2.  
  3. Int pthread_join (pthread_t thread, void ** status );
  4.  
  5. Parameter 1: thread ID
  6.  
  7. Parameter 2: Save the return value of the thread entry function

Instance code:

 
 
  1. # Include
  2.  
  3. # Include
  4.  
  5. # Include
  6.  
  7. # Include
  8.  
  9. Void * thread_main (void * arg );
  10.  
  11. Int main (int argc, char * argv [])
  12.  
  13. {
  14.  
  15. Pthread_t t_id;
  16.  
  17. Int thread_param = 5;
  18.  
  19. Void * thr_ret;
  20.  
  21. // Create a thread
  22.  
  23. If (pthread_create (& t_id, NULL, thread_main, (void *) & thread_param )! = 0)
  24.  
  25. {
  26.  
  27. Puts ("pthread_create () error ");
  28.  
  29. Return-1;
  30.  
  31. }
  32.  
  33. // Wait for the thread to return
  34.  
  35. If (pthread_join (t_id, & thr_ret )! = 0)
  36.  
  37. {
  38.  
  39. Puts ("pthread_join () error ");
  40.  
  41. Return-1;
  42.  
  43. }
  44.  
  45. Printf ("Thread return message: % s \ n", (char *) thr_ret );
  46.  
  47. Free (thr_ret );
  48.  
  49. Return 0;
  50.  
  51. }
  52.  
  53. // Thread entry function
  54.  
  55. Void * thread_main (void * arg)
  56.  
  57. {
  58.  
  59. Int I;
  60.  
  61. Int cnt = * (int *) arg );
  62.  
  63. Char * msg = (char *) malloc (sizeof (char) * 50 );
  64.  
  65. Strcpy (msg, "Hello, I am thread ~ \ N ");
  66.  
  67. For (I = 0; I <cnt; I ++)
  68.  
  69. {
  70.  
  71. Puts ("running thread ");
  72.  
  73. }
  74.  
  75. Return (void *) msg;
  76.  
  77. }


Related Article

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.