3 Ubuntu上使用Qt运行多线程,设置程序自启动及保护脚本

多线程

使用多线程时我们需要加入#include< thread >,这个头文件包含多线程的一些函数
#include< unistd >,这个头文件包含延时函数,sleep(time) eg:sleep(1) //延时1s
C++类:

 class Father
{
public:
    void Father_run()
    {
        while(1)
        {
			for(int i=0;i<3;i++)
			{
            cout<<"Father is running"<<endl;
            sleep(1);
			}
				break;
        }
    }

    void Mather_run()
    {
        while(1)
        {
			for(int i=0;i<3;i++)
			{
            cout<<"Mather is running"<<endl;
            sleep(1);
			}
			break;
        }
    }
};

主函数:

 int main()
{

    Father father;
    std::thread t1(&Father::Father_run,std::ref(father));
    std::thread t2(&Father::Mather_run,std::ref(father));
    t1.join();
    t2.join();


    cout << "Hello World!" << endl;
    return 0;
}

程序运行后,Father_run和Mather_run两个函数会同时运行,并一直输出。
注意:在qt中使用多线程时需要在qt的工程配置文件,也就是pro文件中,加入
LIBS +=-lpthread(添加多线程的库)
在这里插入图片描述
一定要添加,否则会报错。

自启动及保护脚本

Shell教程:https://www.runoob.com/linux/linux-shell.html

自启动及保护脚本:

#!/bin/bash        
echo "Hello !!! "
echo "Start vision code"                             
src=1                                           
cnt=0                                                   
name=FeiHuVision_optimize
cd /.../.../file
make clean && make -j                                    #清除,编译
while [ 1 ]                    
do
count=`ps -ef | grep $name | grep -v "grep" | wc -l`     #返回目标进程数量(正常是2)
echo "Thread count: $count"                              #打印信息
echo "Expection count: $cnt"                             #打印信息
if [ $count -gt 1 ]; then                                #如果进程数>1则表示程序运行正常
	echo "The $name is still alive!"
	sleep $src
else                                                      
	echo "Starting $name..."                             #运行ttyUSB。sh脚本
	#cd ~ && ./ttyUSB.sh
        #cd /home/s305-nuc5/kevin/build-$name-Desktop-Debug/ 
    gnome-terminal -x bash -c "./$name;exec bash;"       #打开一个新的终端,运行程序
    echo "$name has started!"		
	sleep $src                                           #延时sec秒
	((cnt=cnt+1))                                        #cnt自加1
	if [ $cnt -gt 9 ]; then                              #如果cnt>9,重启
	echo "reboot_now"
	fi
fi
done

结束自启动脚本:

#!/bin/bash
echo "Hello !!!"
echo "Kill vision code"
pkill autorun.sh

脚本程序简单说明:

ps -ef //用于查看全格式的全部进程
ps -ef | grep shell_test //shell_test是程序运行的名字。功能是查询有关shell_test的所有进程。
ps -ef | grep $name | grep -v “grep”//功能是去除包含”grep”文本的所有有关shell_test的进程。
wc -l //只显示行数并返回。
ps -ef | grep $name | grep -v “grep” | wc -l //返回进程数
gnome-terminal -x bash -c “./shell_test;exec bash;” //打开一个新的终端,并运行shell_test程序,结束后退出。(最后的exec bash;是让打开的终端在执行完脚本后不关闭。)
&&: eg: 命令1&&命令2 //当命令1运行成功后继续运行2

操作步骤:
切换到脚本目录下:

 sudo chmod u+x shell.sh   //增加执行属性
 bash shell.sh            //用bash解释器运行shell.sh脚本

设置自启动

在终端输入

gnome-session-properties

在这里插入图片描述
添加脚本程序即可
在这里插入图片描述
开机就可以自启动了

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值