学习C++笔记400

简介: C++ 多线程

实例

#include<iostream>#include<cstdlib>#include<pthread.h>#include<unistd.h>usingnamespacestd;  #defineNUM_THREADS     5void *wait(void *t){   inti;    longtid;      tid = (long)t;      sleep(1);    cout << "Sleeping in thread " << endl;    cout << "Thread with id : " << tid << "  ...exiting " << endl;    pthread_exit(NULL);}intmain(){   intrc;    inti;    pthread_tthreads[NUM_THREADS];    pthread_attr_tattr;    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

Sleepingin thread

Threadwith id :4  ...exiting

Sleepingin thread

Threadwith id :3  ...exiting

Sleepingin thread

Threadwith id :2  ...exiting

Sleepingin thread

Threadwith id :1  ...exiting

Sleepingin thread

Threadwith 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.

目录
相关文章
|
20天前
|
C++
c++的学习之路:27、红黑树
c++的学习之路:27、红黑树
30 4
|
20天前
|
存储 C++ 容器
c++的学习之路:26、AVL树
c++的学习之路:26、AVL树
27 0
|
3天前
|
编译器 C++
【C++】继续学习 string类 吧
首先不得不说的是由于历史原因,string的接口多达130多个,简直冗杂… 所以学习过程中,我们只需要选取常用的,好用的来进行使用即可(有种垃圾堆里翻美食的感觉)
7 1
|
3天前
|
算法 安全 程序员
【C++】STL学习之旅——初识STL,认识string类
现在我正式开始学习STL,这让我期待好久了,一想到不用手撕链表,手搓堆栈,心里非常爽
10 0
|
3天前
|
存储 安全 测试技术
【C++】string学习 — 手搓string类项目
C++ 的 string 类是 C++ 标准库中提供的一个用于处理字符串的类。它在 C++ 的历史中扮演了重要的角色,为字符串处理提供了更加方便、高效的方法。
13 0
【C++】string学习 — 手搓string类项目
|
13天前
|
编译器 C语言 C++
【C++入门学习指南】:函数重载提升代码清晰度与灵活性
【C++入门学习指南】:函数重载提升代码清晰度与灵活性
23 0
|
14天前
|
安全 Java 程序员
【C++笔记】从零开始认识继承
在编程中,继承是C++的核心特性,它允许类复用和扩展已有功能。继承自一个基类的派生类可以拥有基类的属性和方法,同时添加自己的特性。继承的起源是为了解决代码重复,提高模块化和可维护性。继承关系中的类形成层次结构,基类定义共性,派生类则根据需求添加特有功能。在继承时,需要注意成员函数的隐藏、作用域以及默认成员函数(的处理。此外,继承不支持友元关系的继承,静态成员在整个继承体系中是唯一的。虽然多继承和菱形继承可以提供复杂的设计,但它们可能导致二义性、数据冗余和性能问题,因此在实际编程中应谨慎使用。
17 1
【C++笔记】从零开始认识继承
|
18天前
|
C++
C++从入门到精通:2.1.2函数和类——深入学习面向对象的编程基础
C++从入门到精通:2.1.2函数和类——深入学习面向对象的编程基础
|
19天前
|
缓存 网络协议 编译器
针对Android系统工程师的C/C++学习目录
针对Android系统工程师的C/C++学习目录
6 0
|
20天前
|
存储 自然语言处理 C++
c++的学习之路:25、map与set
c++的学习之路:25、map与set
15 0