Talk C chestnuts together (122nd back: C language instance-multithreading)

Source: Internet
Author: User

Talk C chestnuts together (122nd back: C language instance-multithreading)

Hello, everyone. We talked about the thread knowledge system diagram in the last time. This example is multithreading. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

Look, I will give you a good old age and wish you good health and good luck in the new year.

Today we will introduce multithreading. I believe everyone has heard of this term, but what does it mean? In fact, multithreading means that two or more threads run together to complete a job together.

We introduced the use of the pthread_create () function to create a thread in the previous chapter. To use multithreading, you only need to use this function multiple times to create a thread. The following is an example.

The following is the core code:

    while(count > 0)    {        switch(count)        {        case 1:            strcpy(param, "This is Thread:1");            break;        case 2:            strcpy(param, "This is Thread:2");            break;        case 3:            strcpy(param, "This is Thread:3");            break;        case 4:            strcpy(param, "This is Thread:4");            break;        case 5:            strcpy(param, "This is Thread:5");            break;        default:            break;        }        res = pthread_create(&thread_value,NULL,thread_func,(void *)param);        if(0 != res)        {            printf("%s ,it can't be created \n",param);            return 1;        }        sleep(1);        count--;    }

In the code, we assign the count value to 5, and then create a thread through a loop statement. In this way, we can create five threads. In addition, these threads share a thread function, but the parameters passed to the function are different. The code for this function is as follows. For details, refer:

void *thread_func(void *param){    int status;    printf("%s \n",(char *)param);    pthread_exit(&status); // end the thread}

The function simply outputs the content of the parameter to indicate that different threads are running and then end the thread.

The readers will not write code in the text, and the completed Code will be put into my resources. You can download and use it.

Below isProgram running resultFor more information, see:

Create multi thread This is Thread: 5 // The Fifth Thread is running This is Thread: 4 // The fourth thread is running This is Thread: 3 // The third Thread is running This is Thread: 2 // The second Thread is running This is Thread: 1 // The first Thread is running

Let's talk about the example of multithreading. We just briefly introduce how to create multiple threads, but the most troublesome thing is how to schedule multiple threads. Because Thread Scheduling involves operating system knowledge, we will not introduce it for the time being. We will have the opportunity to introduce the multi-thread scheduling knowledge in the future. I want to know what examples will be provided later, and I will try again.

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.