QT/C++基础知识_线程demo完整总结

[相关概念]:

        在C++中,线程是一种轻量级的进程,它是程序执行流的最小单元。多线程是指从软件或者硬件上实现多个线程并发执行的技术。

        并行(parallellism)指的是多个任务在同一时刻同时在执行。
        而并发(concurrency)是指在一个时间段内,多个任务交替进行。虽然看起来像在同时执行,但其实是交替的。
        区别:同一时刻和一个时间段

[举例说明]:

#include <iostream>
#include <thread>

using namespace std;

//1)一般的新建线程/
//语法:thread 线程名(线程函数)
//        线程名.join();    
void f()
{
    cout << "thread 1 is running" << endl;
    this_thread::sleep_for(chrono::seconds(1));
}

int main()
{
    thread t1(f); //创建线程,一旦创建完毕,马上开始运行。
    t1.join();
}
//1)输出:thread 1 is running

//2)带函数参数的线程/
//语法:thread 线程名(线程函数,线程函数的参数)
//        线程名.join();    
//当需要向线程函数传递参数时,直接在创建线程时,同时也把参数作为入参传递给线程函数。
//注意当调用函数的参数为引用参数时,线程调用需要加上ref关键字表示引用。并且线程函数会改变引用的变量值。
void f1(int n)
{
    n++;
    cout << "n = " << n << endl;
}

void f2(int &n)//ref表示引用,n的变量值会传递给主调函数
{
    n++;
    cout << "n = " << n << endl;
}

int main()
{
    int n = 0;

    thread t1(f1, n);
    t1.join();
    cout << "n = " << n << endl;

    thread t2(f2, ref(n));
    t2.join();
    cout << "n = " << n << endl;
}

//2)输出:n = 1
//n = 0
//n = 1
//n = 1


//3)替换线程/
//语法:thread 线程名(move(被替换线程函数名))
//        线程名.join();    
void f2(int &n)
{
    n++;
    cout << "n = " << n << endl;
}

int main()
{
    int n = 0;

    thread t3(f2, ref(n));
    thread t4(move(t3)); //此时t4正在运行f2(),t3不再是一个线程了。
    t4.join();
}
//3)输出:n = 1


//4)调用类成员函数/
//1、调用类成员非静态函数
//thread 线程名(&类名::函数名,&实例变量,参数)
//        线程名.join();    
//2、调用类成员静态函数
//thread 线程名(&类名::函数名,参数)
//        线程名.join();    
//区别在于调用非静态类成员函数时,需要加上实例变量
class foo
{
public:
    void bar1(int n)
    {
        cout << "n = " << n << endl;
    }
    static void bar2(int n)
    {
        cout << "static function is running" << endl;
        cout << "n = " << n << endl;
    }
};

int main()
{
    foo f;
    thread t1(&foo::bar1, &f, 5); //注意在调用非静态类成员函数时,需要加上实例变量。
    t1.join();

    thread t2(&foo::bar2, 4);
    t2.join();
}
//4)输出:
//n = 5
//static function is running
//n = 4

//5)线程操作之join的理解/
//join的意思就是等待子进程完成,再继续主进程。即使线程在调用join前已经执行完毕,join也是有用的。
void f1(int n)
{
    cout << "thread " << n << " is runnings" << endl;
}


int main()
{
    thread t1(f1, 1); //开始运行线程t1
    thread t2(f1, 2); //开始运行线程t2
    thread t3(f1, 3); //开始运行线程t3

    t1.join(); //等待线程t1执行完毕
    t2.join(); //等待线程t2执行完毕
    t3.join(); //等待线程t3执行完毕
}

//5)输出:
//thread 1 is runningthread thread 3 is running
//2 is running

//5)注意用多个线程同时启动后,实际是并发执行。由cpu的控制台来决定当前调用哪个线程。所以实际运行过程可能会多次交替,
//线程1 - 线程2 - 线程1 - 线程3 - 线程1.....所以得到的结果是混乱的。
//而每次运行的结果都可能都不一样

//6)假如需要在运行线程t2之前,结果t1,怎么办?/
//那么就可以在t2之前,执行t1.join()。
void f1(int n)
{
    cout << "thread " << n << " is running" << endl;
}


int main()
{
    thread t1(f1, 1); //开始运行线程t1
    t1.join(); //等待线程t1执行完毕

    thread t2(f1, 2); //开始运行线程t2
    thread t3(f1, 3); //开始运行线程t3

    t2.join(); //等待线程t2执行完毕
    t3.join(); //等待线程t3执行完毕
}
//6)输出:
//thread 1 is running
//thread 2 is running
//thread 3 is running


7)detach/
detach操作可以将线程分离,允许线程独立执行。等到线程执行完毕后,系统会自动将资源回收。
void independentThread()
{
    cout << "start concurrent thread" << endl;
    cout << "Exiting concurrent thread" << endl;
}

void threadCaller()
{
    cout << "Start thread caller" << endl;
    thread t(independentThread);
    t.detach(); //将线程分离
    //t.join();//子线程插进来,主线程要等子线程完成后再继续主线程
    cout << "Exiting thread caller" << endl;
}

int main()
{
    threadCaller();
}

//t.detach()时的输出:
//Start thread caller
//start concurrent thread
//Exiting thread caller
//Exiting concurrent thread

//t.join()时的输出:
//Start thread caller
//start concurrent thread
//Exiting concurrent thread
//Exiting thread caller
//区别在于join是要求子线程完成后再进行主线程
//detach是子线程和主线程各玩各的
//注意:每个线程要么detach,独立运行,然后系统自动回收资源;要么join,也就是让主线程等待子线程结束之后,主线程将资源回收。
//如果两个操作都不执行,可能会出现内存泄漏。

//8)线程暂停///
//如果让线程从外部暂停会引发很多并发问题,这也是为什么std::thread没有直接提供pause函数的原因。
//如果线程在运行过程中,确实需要停顿,就可以用this_thread::sleep_for。
void threadCaller()
{
    this_thread::sleep_for(chrono::seconds(3)); //此处线程停顿3秒。
    cout << "thread pause for 3 seconds" << endl;
}

int main()
{
    thread t(threadCaller);
    t.join();
}
//停顿3s后输出:
//thread pause for 3 seconds

//9)获取当前线程号this_thread::get_id();
int main()
{
    thread::id main_threadId = this_thread::get_id();
    cout << main_threadId << endl;
}
//输出:16632

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值