zoukankan      html  css  js  c++  java
  • c++11 跨平台多线程demo和qt 静态链接(std::thread有join函数,设置 QMAKE_LFLAGS = -static)


    #include <stdio.h>
    #include <stdlib.h>

    #include <chrono> // std::chrono::seconds
    #include <iostream> // std::cout
    #include <thread> // std::thread, std::this_thread::sleep_for

    //http://www.cnblogs.com/haippy/p/3236136.html
    void thread_task(int n) {
    std::this_thread::sleep_for(std::chrono::seconds(n));
    std::cout << "hello thread "
    << std::this_thread::get_id()
    << " paused " << n << " seconds" << std::endl;
    }

    /*
    * === FUNCTION =========================================================
    * Name: main
    * Description: program entry routine.
    * ========================================================================
    */
    int main(int argc, const char *argv[])
    {
    std::thread threads[5];
    std::cout << "Spawning 5 threads... ";
    for (int i = 0; i < 5; i++) {
    threads[i] = std::thread(thread_task, i + 1);
    }
    std::cout << "Done spawning threads! Now wait for them to join ";
    for (auto& t: threads) {
    t.join();
    }
    std::cout << "All threads joined. ";

    return EXIT_SUCCESS;
    } /* ---------- end of function main ---------- */


    #include <iostream>
    #include <utility>
    #include <thread>
    #include <chrono>
    #include <functional>
    #include <atomic>

    //http://en.cppreference.com/w/cpp/thread/thread/thread
    void f1(int n)
    {
    for (int i = 0; i < 5; ++i) {
    std::cout << "Thread 1 executing ";
    ++n;
    std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    }

    void f2(int& n)
    {
    for (int i = 0; i < 5; ++i) {
    std::cout << "Thread 2 executing ";
    ++n;
    std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    }

    int main()
    {
    int n = 0;
    std::thread t1; // t1 is not a thread
    std::thread t2(f1, n + 1); // pass by value
    std::thread t3(f2, std::ref(n)); // pass by reference
    std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
    t2.join();
    t4.join();
    std::cout << "Final value of n is " << n << ' ';
    }
    <pre name="code" class="cpp">TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt

    QMAKE_CXXFLAGS += -std=c++11

    QMAKE_LFLAGS  = -static

    SOURCES += main.cpp

    unix{
        #linux add pthread
        LIBS +=  -lpthread
    }

    include(deployment.pri)
    qtcAddDeployment()

    ---------------------
    作者:yunshouhu
    来源:CSDN
    原文:https://blog.csdn.net/earbao/article/details/52809046
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    TextView autoLink不识别大写url问题
    用ImageLoader取拍摄的照片到imageView里面 图片的方向不对问题
    android使用qrcode_swetake.jar生成二维码
    jenkins + Git 搭建持续集成环境
    win10+jenkins+git+自动发布(搭建+构建)
    springMvc项目配置步骤
    linux系统下安装Jenkins
    解决java compiler level does not match the version of the installed java project facet
    Java通过FTP服务器上传下载文件的方法
    Nexus 安装(Linux 环境)
  • 原文地址:https://www.cnblogs.com/findumars/p/10974633.html
Copyright © 2011-2022 走看看