TCP IP network programming: Chapter 18 implementation of multithreaded server

Compared with multi process model, multi thread model has the following advantages:

  1. No need to switch data area and heap during context switching, which reduces the cost of context switching
  2. Data area and heap area can be used to exchange data

Process: the unit that forms a separate execution flow in the operating system

Thread: the unit forming a separate execution flow in a process

 

Thread creation and execution process

#include <pthread.h>

int pthread_create
(
    pthread_t* restrict thread,      // Save the variable address value of the newly created thread ID
    const pthread_attr_t* restrict attr,  // Thread property, default if NULL
    void* (* start_routine)(void *),  // Equivalent to the address value of the thread main function 
    void* restrict arg   //Passing the address value of the variable passing the parameter information when calling the function through the third parameter
);

Let's use a simple program to demonstrate the use of pthread ﹣ create

// thread1.c

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void* thread_main(void *arg);

int main(int argc, char* argv[])
{
    pthread_t t_id;
    int thread_param = 5;

    if(pthread_create(&t_id, NULL, thread_main, (void*)&thread_param) != 0)
    {
        puts("pthread_create() error");
        return -1;
    }
    sleep(6);
    puts("end of main");
    
    return 0;
}

void* thread_main(void* arg)
{
    int i;
    int cnt=*((int*)arg);
    for(i=0; i<cnt; ++i)
    {
        sleep(1);
        puts("running thread");
    }

    return NULL;
}

The following figure shows the operation effect

 

In the above program, we estimate the thread end time by adding sleep to the main function. This method is unreliable,

The pthread Ou join function enables the process calling the function to wait until the thread with the first parameter ID terminates.

In addition, you can get the main function return value of the thread

#include <pthread.h>
int pthread_join
(
    pthread_t thread, // ID of the waiting thread
    void** status     // Pointer variable address value of return value of thread mian function
);

// Success returned 0, failure returned - 1

 

Here is the program that uses pthread Ou join

// thread2.c

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

void* thread_main(void *arg);

int main(int argc, char* argv[])
{
    pthread_t t_id;
    int thread_param = 5;
    void* thr_ret;

    if(pthread_create(&t_id, NULL, thread_main, (void*)&thread_param) != 0)
    {
        puts("pthread_create() error");
        return -1;
    }
    
    if(pthread_join(t_id, &thr_ret) != 0)
    {
        puts("pthread_join() error");
        return -1;
    }

    printf("Thread return message: %s \n", (char*)thr_ret);
    free(thr_ret);
    return 0;
}

void* thread_main(void* arg)
{
    int i;
    int cnt=*((int*)arg);
    char* msg = (char*)malloc(sizeof(char) * 50);
    strcpy(msg, "Hello, I'amm thread ~ \n");

    for(i=0; i<cnt; ++i)
    {
        sleep(1);
        puts("running thread");
    }

    return (void*)msg;
}

The operation effect is as follows:

 

 

 

Posted by nepzap2 on Fri, 15 Nov 2019 13:04:34 -0800