C++线程的创建简单应用

一 点睛

1 pthread_create的用法

https://baike.baidu.com/item/pthread_create/5139072?fr=aladdin

二 创建一个简单的线程,不传参数

1 代码

#include <pthread.h>
#include <stdio.h>
#include <unistd.h> //sleep

void *thfunc(void *arg)   //线程函数
{
    printf("in thfunc\n");
    return (void *)0;
}
int main(int argc, char *argv [])
{
    pthread_t tidp;
    int ret;

    ret = pthread_create(&tidp, NULL, thfunc, NULL);    //创建线程
    if (ret)
    {
        printf("pthread_create failed:%d\n", ret);
        return -1;
    }
     
    sleep(1);  // main线程挂起1秒钟,为了让子线程有机会执行
    printf("in main:thread is created\n");
     
    return 0;
}

2 运行

[root@localhost test]# g++ -o test test.cpp -lpthread
[root@localhost test]# ./test
in thfunc
in main:thread is created

3 说明

这个例子中,首先创建一个线程,在线程函数中打印一行字符串后结束,而主线程在创建子线程后,会等待一秒,这样不至于因为主线程过早结束而导致进程结束,进程结束后,子线程就没机会执行了。如果没有等待函数sleep,则可能子线程的线程函数还没来得及执行,主线程就结束了,这样导致了子线程的线程函数都没有机会执行,因为主线程已经结束,整个应用程序已经退出了。

二 创建一个线程,并传入整型参数

1 代码

#include <pthread.h>
#include <stdio.h>

void *thfunc(void *arg)
{
    int *pn = (int*)(arg); //获取参数的地址
    int n = *pn;
     
    printf("in thfunc:n=%d\n", n);
    return (void *)0;
}
int main(int argc, char *argv [])
{
    pthread_t tidp;
    int ret, n=110;

    ret = pthread_create(&tidp, NULL, thfunc, &n);//创建线程并传递n的地址
    if (ret)
    {
        printf("pthread_create failed:%d\n", ret);
        return -1;
    }
     
    pthread_join(tidp,NULL); //等待子线程结束
    printf("in main:thread is created\n");
     
    return 0;
}

2 运行

[root@localhost test]# g++ -o test test.cpp -lpthread
[root@localhost test]# ./test
in thfunc:n=110
in main:thread is created

3 说明

这个例子和上面这个例子有两点不同:一是创建线程的时候,把一个整型变量的地址作为参数传给线程函数;二是等待子线程结束没有用sleep函数,而是用pthread_join函数,sleep只是等待一个固定的时间,有可能在固定的时间内子线程早已结束,或者子线程运行时间大于这个固定时间,因此用它来等待子线程结束并不精确,而用函数pthread_join则会一直等到子线程结束后才执行该函数后面的代码。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++多线程可以应用于各种场景,以下是一个简单的多线应用实例: 假设我们有一个任务,需要计算一个数组中所有元素的平方和。我们可以使用多线程来加速计算过程。 首先,我们创建一个包含大量元素的数组,并将其分成几个部分。然后,我们创建多个线程,每个线程负责计算其中一部分数组元素的平方和。最后,将每个线程计算得到的结果相加,即可得到整个数组的平方和。 下面是一个简单的示例代码: ```cpp #include <iostream> #include <thread> #include <vector> // 计算数组部分元素的平方和 int calculateSum(const std::vector<int>& arr, int start, int end) { int sum = 0; for (int i = start; i < end; ++i) { sum += arr[i] * arr[i]; } return sum; } int main() { std::vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int numThreads = 4; // 假设使用4个线程 std::vector<std::thread> threads; std::vector<int> partialSums(numThreads); // 创建并启动多个线程 for (int i = 0; i < numThreads; ++i) { int start = i * arr.size() / numThreads; int end = (i + 1) * arr.size() / numThreads; threads.emplace_back([start, end, &arr, &partialSums, i]() { partialSums[i] = calculateSum(arr, start, end); }); } // 等待所有线程完成 for (auto& thread : threads) { thread.join(); } // 计算最终结果 int totalSum = 0; for (int sum : partialSums) { totalSum += sum; } std::cout << "数组的平方和为:" << totalSum << std::endl; return 0; } ``` 在上述示例中,我们使用了`std::thread`来创建多个线程,并使用`std::vector`来保存每个线程计算得到的部分结果。最后,我们将所有部分结果相加得到最终结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值