IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++ | C++多线程编程 -> 正文阅读

[C++知识库]C++ | C++多线程编程

C++ | C++多线程编程

C++ 多线程

多线程是多任务处理的一种特殊形式,多任务处理允许让电脑同时运行两个或两个以上的程序。
一般情况下,两种类型的多任务处理:基于进程和基于线程

  • 基于进程的多任务处理程序的并发执行
  • 基于线程的多任务处理同一程序的片段的并发执行

创建线程

PTHREAD_CREATE(3)           Linux Programmer's Manual          PTHREAD_CREATE(3)

NAME
       pthread_create - create a new thread

SYNOPSIS
       #include <pthread.h>

       int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

       Compile and link with -pthread.
RETURN VALUE
       On success, pthread_create() returns 0; on error, it returns an error number, and the contents of *thread are undefined.

thread指向线程标识符的指针
attr设置线程属性,NULL表示缺省
start_routine线程运行函数的起始地址
arg函数运行的参数

线程退出

PTHREAD_EXIT(3)                                                        Linux Programmer's Manual                                                        PTHREAD_EXIT(3)

NAME
       pthread_exit - terminate calling thread

SYNOPSIS
       #include <pthread.h>

       void pthread_exit(void *retval);

       Compile and link with -pthread.

DESCRIPTION
       The  pthread_exit() function terminates the calling thread and returns a value via retval that (if the thread is joinable) is available to another thread in the
       same process that calls pthread_join(3).

       Any clean-up handlers established by pthread_cleanup_push(3) that have not yet been popped, are popped (in the reverse of the order in which they  were  pushed)
       and  executed.   If  the  thread  has any thread-specific data, then, after the clean-up handlers have been executed, the corresponding destructor functions are
       called, in an unspecified order.

       When a thread terminates, process-shared resources (e.g., mutexes, condition variables, semaphores, and file descriptors) are not released, and functions regis‐
       tered using atexit(3) are not called.

       After  the last thread in a process terminates, the process terminates as by calling exit(3) with an exit status of zero; thus, process-shared resources are re‐
       leased and functions registered using atexit(3) are called.

RETURN VALUE
       This function does not return to the caller.

ERRORS
       This function always succeeds.

CONFORMING TO
       POSIX.1-2001.

NOTES
       Performing a return from the start function of any thread other than the main thread results in an implicit call to pthread_exit(), using the function's  return
       value as the thread's exit status.

       To allow other threads to continue execution, the main thread should terminate by calling pthread_exit() rather than exit(3).

       The value pointed to by retval should not be located on the calling thread's stack, since the contents of that stack are undefined after the thread terminates.

BUGS
       Currently, there are limitations in the kernel implementation logic for wait(2)ing on a stopped thread group with a dead thread group leader.  This can manifest
       in problems such as a locked terminal if a stop signal is sent to a foreground process whose thread group leader has already called pthread_exit().

SEE ALSO
       pthread_create(3), pthread_join(3), pthreads(7)

COLOPHON
       This page is part of release 3.55 of the Linux man-pages project.  A description of the  project,  and  information  about  reporting  bugs,  can  be  found  at
       http://www.kernel.org/doc/man-pages/.

Linux                                                                          2009-03-30                                                               PTHREAD_EXIT(3)

实例1

/*******************************************************************
 *   > File Name: thread.cpp
 *   > Create Time: 2021年09月24日 11:50:02
 ******************************************************************/
#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;

#define NUM_PTHREAD     (5)

void* say(void *arg)
{
    cout << "thread, arg:" << *(int*)arg << endl;
    return NULL;
}

int main(int argc, char* argv[])
{
    pthread_t thread[NUM_PTHREAD]; /* 定义5个线程标识符 */
    for(int i; i< NUM_PTHREAD; i++)
    {
        pthread_create(&thread[i], NULL, say, (void*)&i); /* 创建线程 */
        sleep(1);
    }
    pthread_exit(NULL); /* 结束线程 */

    return 0;
}

编译、运行:

PS E:\fly-prj\cplusplus\day18> make 
g++ -o thread thread.cpp -g -Wall -O0 -lpthread
PS E:\fly-prj\cplusplus\day18> .\thread.exe
thread, arg:1
thread, arg:2
thread, arg:3
thread, arg:4

实例2

/*******************************************************************
 *   > File Name: thread1.cpp
 *   > Create Time: 2021年09月24日 12:41:44
 ******************************************************************/
#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;

#define THREAD_NUM      (5)

void *say(void *args)
{
    cout << " thread ID: " << *((int *)args) << endl;
    pthread_exit(NULL);
    return (void *)0;
}

int main(int argc, char* argv[])
{
    pthread_t thread[THREAD_NUM];
    int indexs[THREAD_NUM];
    for(int i = 0; i < THREAD_NUM; ++i)
    {
        cout << "main(), create thread " << i << endl;
        indexs[i] = i;
        if(pthread_create(&thread[i], NULL, say, (void *)&(indexs[i])))
        {
            cout << "pthread_create error." << endl;
            return (-1);
        }
        sleep(1);
    }
    pthread_exit(NULL);

    return 0;
}

编译、运行:

PS E:\fly-prj\cplusplus\day18> make 
g++ -o thread1 thread1.cpp -g -Wall -O0 -lpthread
PS E:\fly-prj\cplusplus\day18> .\thread1.exe
main(), create thread 0
 thread ID: 0
main(), create thread 1
 thread ID: 1
main(), create thread 2
 thread ID: 2
main(), create thread 3
 thread ID: 3
main(), create thread 4
 thread ID: 4

向线程传递参数

实例3

/*******************************************************************
 *   > File Name: thread_args.cpp
 *   > Create Time: 2021年09月24日 13:03:15
 ******************************************************************/
#include <iostream>
#include <unistd.h>
#include <pthread.h>
using namespace std;

#define THREAD_NUM      (5)
typedef struct thread_args{
    int id;
    char *name;
}THREAD_ARGS, *pTHREAD_ARGS;

void* say(void *args)
{	/* 传递多个参数 */
    cout << "id: " << ((pTHREAD_ARGS)args)->id << endl;
    cout << "name: " << ((pTHREAD_ARGS)args)->name << endl;
    pthread_exit(NULL);
}

int main(int argc, char* argv[])
{
    THREAD_ARGS args[THREAD_NUM];
    pthread_t threadid[THREAD_NUM];

    for(int i=0; i< THREAD_NUM; i++){
       args[i].id = i;
       args[i].name = "Thread test."; 
       threadid[i] = (pthread_t)i;
       if(pthread_create(&threadid[i], NULL, say, (void*)&args[i])){
           cout << "pthread_create error." << endl;exit(-1);
       }
       sleep(1); // 不加,可能出现栈溢出
    }
    pthread_exit(NULL);

    return 0;
}

编译、运行:

PS E:\fly-prj\cplusplus\day18> make 
g++ -o thread_args thread_args.cpp -g -Wall -O0 -lpthread
thread_args.cpp: 在函数‘int main(int, char**)’中:
   33 |        args[i].name = "Thread test.";
      |                       ^~~~~~~~~~~~~~
thread_args.cpp:34:22: 警告:将一个整数转换为大小不同的指针 [-Wint-to-pointer-cast]
   34 |        threadid[i] = (pthread_t)i;
      |                      ^~~~~~~~~~~~
PS E:\fly-prj\cplusplus\day18> .\thread_args.exe
id: 0
name: Thread test.
id: 1
name: Thread test.
id: 2
name: Thread test.
id: 3
name: Thread test.
id: 4
name: Thread test.

连接和分离线程

int pthread_join(pthread_t thread, void **retval); //加入一个终止的线程

int pthread_detach(pthread_t thread); //分离线程

pthread_join()子程序阻碍调用程序,直到指定的 threadid 线程终止为止。当创建一个线程时,它的某个属性会定义它是否是可连接的(joinable)或可分离的(detached)。只有创建时定义为可连接的线程才可以被连接。如果线程创建时被定义为可分离的,则它永远也不能被连接。

实例4

/*******************************************************************
 *   > File Name: pthread.cpp
 *   > Create Time: 2021年09月24日 23:43:58
 ******************************************************************/
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <unistd.h>
using namespace std;

#define NUM_THREADS     (5)

void *wait(void *t)
{
    long tid;

    tid = (long)t;
    sleep(1);
    cout << "Sleep in thread " << endl;
    cout << "Thread with id: " << tid << "...exiting" << endl;
    pthread_exit(NULL);
}

int main(int argc, char* argv[])
{
    int rc;
    int i;
    pthread_t threads[NUM_THREADS];
    pthread_attr_t attr;
    void *status;

    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." << 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); /*结束当前线程*/

    return 0;
}

编译、运行:

PS D:\study\cplusplus\day9> make
g++ -o pthread pthread.cpp -g -Wall -lpthread
PS D:\study\cplusplus\day9> .\pthread.exe
main() : creating thread, 0
main() : creating thread, 1
main() : creating thread, 2
main() : creating thread, 3
main() : creating thread, 4
Sleep in thread
Sleep in thread Sleep in thread Sleep in thread Sleep in thread



Thread with id: 4294953992Thread with id: Thread with id: Thread with id: Thread with id: ...exiting4294953992429495399242949539924294953992
...exiting...exiting...exiting...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.

C++ 11 标准线程库(std::thread

五个头文件:

  • <atomic>引入std::atomicstd::atomic_flag,定义了原子操作
  • <thread>声明了std::threadstd::this_thread命名空间
  • <mutex>声明了与互斥量mutex相关的类
  • <condition_variable>声明了与条件变量相关的类
  • <future>

实例5

/*******************************************************************
 *   > File Name: pthread1.cpp
 *   > Create Time: 2021年09月25日 10:29:50
 ******************************************************************/
#include <iostream>
#include <thread>
using namespace std;

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";
    }else{
        std::cout << "This is not the main thread.";
    }
}

void pause_thread(int n){
    std::this_thread::sleep_for(std::chrono::seconds(n));
    std::cout << "pause of " << n << "seconds ended\n";
}

int main(int argc, char* argv[])
{
    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" << endl;
    for(int i = 0; i< 5; i++){
        threads[i] = std::thread(pause_thread, i+1);
    }
    std::cout << "Done spawning threads. Now waiting for them to join:\n";
    for(auto &thread:threads){
        thread.join();
    }
    std::cout << "All threads joined\n";

    return 0;
}

编译、运行:

PS D:\study\cplusplus\day9> make
g++ -o pthread pthread.cpp -g -Wall -std=c++11 -lpthread
g++ -o pthread1 pthread1.cpp -g -Wall -std=c++11 -lpthread
PS D:\study\cplusplus\day9> .\pthread1.exe
Hello Concurrent World
4This is not the main thread.
native_handle0x800074630
Hello Concurrent World
Spawning 5 threads...
This is not the main thread.
Done spawning threads. Now waiting for them to join:
pause of 1seconds ended
pause of 2seconds ended
pause of 3seconds ended
pause of 4seconds ended
pause of 5seconds ended
All threads joined

std::thread构造函数

默认构造函数thread() noexceped;
初始化构造函数template <class Fn, class ...Args> explicit thread(Fn&& fn, Args&&...args);
拷贝构造函数[delected]thread(const thread &) = delete;
Move构造函数thread(thread&& x) noexcept;
  • 默认构造函数,创建一个空的std::thread执行对象。
  • 初始化构造函数,创建一个std::thread对象,该std::thread对象可被joinable,新产生的线程会调用fn函数,该函数的参数由args给出。
  • 拷贝构造函数(被禁用),意味着std::thread对象不可拷贝构造。
  • Move构造函数,move构造函数(move语义是C++11新出现的概念,详见附录),调用成功后x不代表任何std::thread执行对象。

注意:可被 joinable std::thread 对象必须在他们销毁之前被主线程 join 或者将其设置为 detached.

实例6

/*******************************************************************
 *   > File Name: std_thread.cpp
 *   > Create Time: 2021年09月25日 18:08:54
 ******************************************************************/

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

using namespace std;

void f1(int n){
    for(int i = 0; i< 5; ++i){
        std::cout << "Thread f1 " << n << " 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 f2 executing\n";
        ++n;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}

int main(int argc, char* argv[])
{
    int n = 0;
    std::thread t1; // t1 is not a thread ,创建一个空的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";

    return 0;
}

编译、运行:

PS D:\study\cplusplus\day9> make
g++ -o std_thread std_thread.cpp -g -Wall -std=c++11 -lpthread
PS D:\study\cplusplus\day9> .\std_thread.exe
Thread f1 1 executing
Thread f2 executing
Thread f1 1 executing
Thread f2 executing
Thread f1 1 executing
Thread f2 executing
Thread f1 1 executing
Thread f2 executing
Thread f1 1 executing
Thread f2 executing
Final value of n is 5

std::thread 赋值操作

Move赋值操作thread& operator=(thread&& rhs) noexcept;
拷贝赋值操作 [delected]thread& operator=(const thread&) = delete;
  • Move赋值操作(1),如果当前对象不可用joinable,需传递一个右值引用(rhs)给move赋值操作;如果当前对象可被joinable,则会调用terminate()报错。
  • 拷贝赋值操作(2),被禁用,因此std::thread对象不可拷贝赋值。

c 多线程编程01

[线程模型、pthread 系列函数 和 简单多线程服务器端程序]

C++11中=delete的巧妙用法

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-09-26 09:58:18  更:2021-09-26 09:58:26 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 20:08:17-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码