c++多线程使用

1.c++ thread

点击查看代码
#include <iostream>
#include <thread>
void M_threadFun()
{
    std::cout<<"now thread begin running"<<std::endl;
    for(int i=0;i<10;++i)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    std::cout<<"now thread should quit"<<std::endl;
}
int main()
{
    //this is main thread
    std::cout<<"this is in main"<<std::endl;
    auto th  = new std::thread(M_threadFun);
    th->join();
    std::cout<<"now in main"<<std::endl;
}

2.#pragma omp parallel关键字
该关键字也可以实现多线程的功能,但是需要注意:#pragma omp parallel关键字创建多线程必须在编译时加上-fopenmp选项

点击查看代码
(1)#pragma omp parallel for

         for()

(2)#pragma omp parallel

        {//注意:大括号必须要另起一行

         #pragma omp for

          for()

        }

原文链接: https://www.cnblogs.com/52ld/p/17140942.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++多线程使用

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/319416

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月24日 下午3:18
下一篇 2023年2月24日 下午3:18

相关推荐