-seventy-
Welcome to my blog space.
/*
    <线程>

    1.什么是多线程?
        多线程是"多任务处理"的一种特殊形式
        多线程程序包含可以同时运行的两个或多个部分
        这样的程序中的每个部分称为一个线程
        每个线程定义了一个单独的执行路径

    2.创建线程
        e.g.
            #include <pthread.h>
            pthread_create (thread, attr, start_routine, arg)

        参数说明:
            参数                    描述
            thread                指向线程标识符指针。
            attr                一个不透明的属性对象,可以被用来设置线程属性。您可以指定线程属性对象,也可以使用默认值 NULL。
            start_routine        线程运行函数起始地址,一旦线程被创建就会执行。
            arg                    运行函数的参数。它必须通过把引用作为指针强制转换为 void 类型进行传递。如果没有传递参数,则使用 NULL。

        创建线程成功时,函数返回 0,若返回值不为 0 则说明创建线程失败
*/

#include <iostream>
// 必须的头文件
#include <pthread.h>

using namespace std;

#define NUM_THREADS 5

// 线程的运行函数
void* say_hello(void* args)
{
    cout << "Hello Runoob!" << endl;
    return 0;
}

int main()
{
    // 定义线程的 id 变量,多个变量使用数组
    pthread_t tids[NUM_THREADS];
    for (int i = 0; i < NUM_THREADS; ++i)
    {
        //参数依次是:创建的线程id,线程参数,调用的函数,传入的函数参数
        int ret = pthread_create(&tids[i], NULL, say_hello, NULL);
        if (ret != 0)
        {
            cout << "pthread_create error: error_code=" << ret << endl;
        }
    }
    //等各个线程退出后,进程才结束,否则进程强制结束了,线程可能还没反应过来;
    pthread_exit(NULL);
}

 

posted on 2023-07-31 18:34  -seventy-  阅读(13)  评论(0)    收藏  举报