文章目录

  • 目录
    • 1.多线程
    • 2.web编程

目录

1.多线程


#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);
}


#include <iostream>
#include <cstdlib>
#include <pthread.h>using namespace std;#define NUM_THREADS     5struct thread_data{int  thread_id;char *message;
};void *PrintHello(void *threadarg)
{struct thread_data *my_data;my_data = (struct thread_data *) threadarg;cout << "Thread ID : " << my_data->thread_id ;cout << " Message : " << my_data->message << endl;pthread_exit(NULL);
}int main ()
{pthread_t threads[NUM_THREADS];struct thread_data td[NUM_THREADS];int rc;int i;for( i=0; i < NUM_THREADS; i++ ){cout <<"main() : creating thread, " << i << endl;td[i].thread_id = i;td[i].message = (char*)"This is message";rc = pthread_create(&threads[i], NULL,PrintHello, (void *)&td[i]);if (rc){cout << "Error:unable to create thread," << rc << endl;exit(-1);}}pthread_exit(NULL);
}


#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <unistd.h>using namespace std;#define NUM_THREADS     5void *wait(void *t)
{int i;long tid;tid = (long)t;sleep(1);cout << "Sleeping in thread " << endl;cout << "Thread with id : " << tid << "  ...exiting " << endl;pthread_exit(NULL);
}int main ()
{int rc;int i;pthread_t threads[NUM_THREADS];pthread_attr_t attr;void *status;// 初始化并设置线程为可连接的(joinable)pthread_attr_init(&attr);pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);for( i=0; i < NUM_THREADS; i++ ){cout << "main() : creating thread, " << i << endl;rc = pthread_create(&threads[i], NULL, wait, (void *)&i );if (rc){cout << "Error:unable to create thread," << rc << endl;exit(-1);}}// 删除属性,并等待其他线程pthread_attr_destroy(&attr);for( i=0; i < NUM_THREADS; i++ ){rc = pthread_join(threads[i], &status);if (rc){cout << "Error:unable to join," << rc << endl;exit(-1);}cout << "Main: completed thread id :" << i ;cout << "  exiting with status :" << status << endl;}cout << "Main: program exiting." << endl;pthread_exit(NULL);
}

当上面的代码被编译和执行时,它会产生下列结果:
main() : creating thread, 0
main() : creating thread, 1
main() : creating thread, 2
main() : creating thread, 3
main() : creating thread, 4
Sleeping in thread
Thread with id : 4 …exiting
Sleeping in thread
Thread with id : 3 …exiting
Sleeping in thread
Thread with id : 2 …exiting
Sleeping in thread
Thread with id : 1 …exiting
Sleeping in thread
Thread with id : 0 …exiting
Main: completed thread id :0 exiting with status :0
Main: completed thread id :1 exiting with status :0
Main: completed thread id :2 exiting with status :0
Main: completed thread id :3 exiting with status :0
Main: completed thread id :4 exiting with status :0
Main: program exiting.

#include <iostream>#include <thread>std::thread::id main_thread_id = std::this_thread::get_id();void hello()
{std::cout << "Hello Concurrent World\n";if (main_thread_id == std::this_thread::get_id())std::cout << "This is the main thread.\n";elsestd::cout << "This is not the main thread.\n";
}void pause_thread(int n) {std::this_thread::sleep_for(std::chrono::seconds(n));std::cout << "pause of " << n << " seconds ended\n";
}int main() {std::thread t(hello);std::cout << t.hardware_concurrency() << std::endl;//可以并发执行多少个(不准确)std::cout << "native_handle " << t.native_handle() << std::endl;//可以并发执行多少个(不准确)t.join();std::thread a(hello);a.detach();std::thread threads[5];                         // 默认构造线程std::cout << "Spawning 5 threads...\n";for (int i = 0; i < 5; ++i)threads[i] = std::thread(pause_thread, i + 1);   // move-assign threadsstd::cout << "Done spawning threads. Now waiting for them to join:\n";for (auto &thread : threads)thread.join();std::cout << "All threads joined!\n";
}

2.web编程



c++基础学习(12)--(多线程、Web编程)相关推荐

  1. html语言技术基础,第2章Web编程基础HTML语言技术方案.ppt

    通过Internet浏览世界各地的网络资源,或者要把信息通过Internet以Web方式发布到全球,就必须使用网页.网页就是用户在浏览器上看到的一个个画面.通过网页,即便是一个不懂计算机的人,也能借助 ...

  2. Go基础学习记录 - 编写Web应用程 - 完善Blog Model

    完善Blog Model 前面的章节 我们分别介绍了MySQL.SQLite.PostgreSQL等数据库的使用 本节简单介绍并完善下我们自己的小博客的Model部分,这部分暂时先分享下Blog Mo ...

  3. Java基础学习笔记之网络编程

    Java基础之网络编程 1.网络编程概述 什么是网络编程 指的是在多个设备(计算机)执行,其中的设备使用一个网络中的所有连接到对方编写程序 网络编程的目的 与其他计算机进行通信 网络编程的问题 1.如 ...

  4. Python学习十一:Web编程

    文章目录 一.前提知识 1.1 Http协议 1.2 Web服务器 1.3 前端知识 1.3.1 HTML 1.3.2 css 1.3.3 JavaScript 1.4 静态服务器和动态服务器 二.W ...

  5. 嵌入式Linux基础学习笔记-文件IO编程-文件锁(2)

    文件操作仿真FIFO,实现生产者-消费者运行模型 编程实现生产者程序producer.c,创建仿真FIFO结构文件(普通文件),按照给定的时间间隔向FIFO文件写入自动生成的字符(自定义),生产周期及 ...

  6. Java基础学习总结:网络编程之(四)TCP与UDP的区别及常见的网络协议(转)

    TCP和UDP的区别 原文:https://blog.csdn.net/li_ning_/article/details/52117463 1.TCP与UDP基本区别 基于连接与无连接 TCP要求系统 ...

  7. 嵌入式Linux基础学习笔记-文件IO编程-I/O多路复用

    实验内容:多路复用-I/O操作及阻塞 编程实现文件描述符集合的监听 multiplex_poll.c文件编写: /* multiplex_poll.c */ #include <fcntl.h& ...

  8. Javascript基础学习12问(四)

    1.错误处理: 什么是错误:导致程序停止运行的运行时异常状态: 错误处理:程序出错,保证程序不停止的机制: ECMAScript六种错误类型: EvalError RangeError:num.toF ...

  9. Java基础学习九 多线程

    一.什么是线程 - 线程(thead)是一个程序内部的一条执行路径. - 程序中如果只有一条执行路径,那么这个程序就是单线程的程序 二.多线程是什么 - 多线程是指从硬件上实现多条执行流程的技术. 三 ...

  10. 网安零基础学习-python-面向对象编程(灰帽编程15)

    一.面向对象的编程 购物机器人 1.收钱100元,酱油 2.过马路,看红路灯 3.给钱,找零 4.交付 这是面向过程的 面向对象: 100元,酱油 你不必考虑过程 二.面向对象的编程 OOP obje ...

最新文章

  1. 干货 | 顾险峰:对抗生成网络的几何理论解释(附视频PPT)
  2. Laravel - Artisan 个人常用总结
  3. 模拟滤波器的单位冲激响应+单位阶跃响应+斜坡响应+抛物线响应matlab实现(转载+整理)
  4. etl介绍与etl工具比较_ETL万岁
  5. PL/Sql快速执行 insert语句的.sql文件
  6. android --- fastboot 协议学习
  7. jquery中去重复排序(函数: $.grep() join() sort() )
  8. EMR 配置纪录(不断更新)
  9. 笔记本系统恢复连载之十:系统恢复并不难
  10. (day 24 - 广度优先搜索 )剑指 Offer 32 - I. 从上到下打印二叉树
  11. AIM Tech Round 5 (rated, Div. 1 + Div. 2)
  12. 2018美赛E题翻译
  13. 服务器多开虚拟机怎么使用教程,游戏多开,你需要这个虚拟机教程
  14. As Giants Step In, Asustek Defends A Tiny PC
  15. 专注世界排名的Alexa.com宣布关站
  16. 程序员如何保护自己的头发
  17. Arduino 工控板开发
  18. UVA 123 Searching Quickly
  19. [Wc2008]游览计划 斯坦纳树
  20. U盘文件突然不见却占内存 解决方案

热门文章

  1. 简单网络聊天程序java_基于Java实现hello/hi简单网络聊天程序
  2. 小程序分享到朋友圈_如何给小程序添加分享朋友圈
  3. python中func函数用法_python之4类回调函数的使用方法
  4. pline加点lisp_用Autolisp 在AutoCAD中实现多种曲线的绘制
  5. 通达oa 不允许从该ip登陆_通达OA-命令执行漏洞复现
  6. Sharepoin学习笔记—架构系列--03 Sharepoint的处理(Process)与执行模型(Trust Model) 2
  7. mysql数据库字符集设置_查看和设置MySQL数据库字符集
  8. python 语句简写_自学Python-语句之列表推导式
  9. 设计模式(六)J2EE 模式
  10. 高通modem启动过程_苹果首次承认正自研基带芯片,高通要被抛弃了?