linux多线程入门教程,Linux下C++ 多线程编程:入门

本文介绍了一个简单的C++多线程程序示例,并详细解释了如何使用线程锁来解决线程间的数据竞争问题。此外,还探讨了线程管理方法,包括detach和join的区别及如何处理线程异常终结。

一个简单的多线程例子

#include

#include

void foo(int i)

{

std::thread::id this_id = std::this_thread::get_id();

std::cout << "thread_func: id = " << this_id << ", " << i << std::endl;

}

int main()

{

for (uint8_t i = 0; i < 4; i++)

{

std::thread t(foo, i);

t.detach();

}

getchar();

return 0;

}

这个程序里面创建了4个线程,每次创建的时候分别传递一个标签进去,运行一次,我们可能会看到如下的输出,

thread_func: id = 139708503934720, 3

thread_func: id = 139708512327424, 2

thread_func: id = 139708520720128, 1

thread_func: id = 139708529112832, 0

可以看到这些线程的调度运行的顺序并不一定按照创建线程的先后,它有可能是任意的;

多运行几次后,我们可能会看到如下的打印

thread_func: id = thread_func: id = 140703835129600, 2

140703851915008, 0

thread_func: id = 140703843522304, 1

thread_func: id = 140703826736896, 3

显然的,第0个线程在打印的时候被第2个线程中断了,所以打印出来是这样。

那怎么解决这个问题呢,我们就引入了线程锁

引入线程锁

修改后的程序如下

#include

#include

#include

std::mutex g_display_mutex;

void foo(int i)

{

std::thread::id this_id = std::this_thread::get_id();

g_display_mutex.lock();

std::cout << "thread_func: id = " << this_id << ", " << i << std::endl;

g_display_mutex.unlock();

}

int main()

{

for (uint8_t i = 0; i < 4; i++)

{

std::thread t(foo, i);

t.detach();

}

getchar();

return 0;

}

这样打印消息的时候就再也不会出现中断的情况了。

detach和join

C++有两种方式来等待线程结束

detach在这里表示启动的线程自己在后台运行,当前的代码继续运行,不等待新的线程结束

join的方式实在线程运行完成后再继续往下运行,所以如果对之前的代码再做修改如下

int main()

{

for (int i = 0; i < 4; i++)

{

std::thread t(foo, i);

t.join();

std::cout << "main: " << i << std::endl;

}

getchar();

return 0;

}

这个时候每次main函数打印的时候都会等待新创建的线程运行完毕,所以固定的会答应如下

thread_func: id = 139945708939008, 0

main: 0

thread_func: id = 139945708939008, 1

main: 1

thread_func: id = 139945708939008, 2

main: 2

thread_func: id = 139945708939008, 3

main: 3

关于线程的异常终结

假如说以detach的方式,让线程在后台运行,那么线程的实例和线程本身就分离开了;有可能出现一种场景,

主函数退出了,所以维护的线程实例也会自然销毁

但是这个时候线程并没有运行完成,会出现异常退出的场景

处理这种问题的方法可以有两种

在线程实例有可能会被销毁前,调用一下join,等待线程完结;

或者通过类的析构函数,创建类的实例的时候创建线程实例,等到销毁这个类的时候会自动调用析构函数,在析构函数里面等待线程运行完结;

class foo

{

public:

foo();

~foo();

private:

std::thread t;

};

foo::foo()

{

t = std::thread([] {

std::thread::id this_id = std::this_thread::get_id();

std::cout << "thread: sleep for 3s! " << std::endl;

sleep(3);

g_display_mutex.lock();

std::cout << "thread id = " << this_id << std::endl;

g_display_mutex.unlock();

});

}

foo::~foo()

{

std::cout << "destructor called" << std::endl;

if (t.joinable())

{

std::cout << "joinable" << std::endl;

t.join();

}

}

int main()

{

foo m_foo;

return 0;

}

运行打印的结果如下,

destructor called

joinable

thread: sleep for 3s!

thread id = 139909607855872

main函数创建完类的实例m_foo后就会退出函数

main函数退出的同时需要销毁m_foo,会自动调用析构函数;

析构函数会等待线程t的运行完成然后再完成类foo的析构;

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值