简体   繁体   English

在C++中同时(同时)运行两个function

[英]Run two function simultaniously (at the same time) in C++

I am programming a robot with QT (C++).我正在使用 QT (C++) 对机器人进行编程。 I have a function move_gripper() which closes or opens the gripper (it depends on the parameter) and a function relative_motion() which lets the end effector move downwards.我有一个 function move_gripper() 可以关闭或打开夹具(取决于参数)和一个 function relative_motion() 可以让末端执行器向下移动。 What I want to do is to close the gripper ( calling the move_gripper() function ) while moving the robot downwards ( calling the relative_motion() function ) The execution time of the functions are not the same ( let's say move_gripper() lasts 2 seconds while relative_motions() lasts 3 seconds ).我想要做的是在向下移动机器人的同时关闭抓手(调用 move_gripper() function)(调用 relative_motion() function)函数的执行时间不一样(比如说 2 秒 move_gripper() 最后而 relative_motions() 持续 3 秒)。 I want the two functions to start the execution at the same time.我希望这两个函数同时开始执行。

In computer theory there is no real "simultaneously".在计算机理论中没有真正的“同时”。 The common approach is to use threads.常用的方法是使用线程。 Those threads are executed, halted, switched just so fast it looks like they are executed simultaneously.这些线程执行、停止、切换的速度非常快,看起来就像是同时执行的。

But still there are some things which can be done wrong which make the usage of threads even worse than not to use them.但是仍然有一些事情可以做错,这使得线程的使用比不使用它们更糟糕。

So I would say first get some knowledge about threads (see the link: https://doc.qt.io/qt-5/qthread.html ) and create 2 threads, one for each channel and do your work in there.所以我想说首先了解一些关于线程的知识(请参阅链接: https://doc.qt.io/qt-5/qthread.html并在每个通道中创建你的线程。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM