#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...\n";
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\n";
for (auto& t: threads) {
t.join();
}
std::cout << "All threads joined.\n";

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";
++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";
++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 << '\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
版权声明:本文为博主原创文章,转载请附上博文链接!

最新文章

  1. MongoDB安全和认证
  2. Leetcode Divide Two Integers
  3. Scalaz(41)- Free :IO Monad-Free特定版本的FP语法
  4. 在CSV文件中增加一列属性值
  5. Env:Cscope安装与配置
  6. zend studio插件
  7. Android 断点续传
  8. cas系列(三)--HTTP和HTTPS、SSL
  9. VBA:Google翻译(含tk算法)
  10. [Cocos2d-x开发问题-3] cocos2dx动画Animation介绍
  11. read table 时关键字TRANSPORTING NO FIELDS的用法
  12. 【MySQL案件】mysql登录-S失败
  13. css3标签学习总结文章
  14. Vim常用命令【转载】
  15. C#设置richtextbox某一段文本颜色
  16. eclipse maven工程打包失败
  17. apache thrift分析
  18. chrome不能浏览任何网页,提示配置proxy,Ubuntu
  19. 多层josn数据 修改
  20. 附001.Docker阿里云Registry加速器配置

热门文章

  1. hashcode(),equal()方法经典分析
  2. java 网络文件下载(并命中文名)
  3. moveUp()
  4. uni-app 的更新及碰到的问题
  5. [Ubuntu]18安装navicat 破解版&amp;官方版本
  6. Vue.js父子组件如何传值 通俗易懂
  7. SSM+form表单文件上传
  8. IDEA如何构建mybatis
  9. 一百一十:CMS系统之剩余菜单栏的页面和视图
  10. Scala语法04 - 其他