C++ 同时运行两个无限循环函数和主函数的程序
时间: 2024-06-14 16:08:50 浏览: 39
在C++中,可以使用多线程来实现同时运行两个无限循环函数和主函数的程序。下面是一个简单的示例代码:
```cpp
#include <iostream>
#include <thread>
// 无限循环函数1
void loop1() {
while (true) {
// 执行一些操作
}
}
// 无限循环函数2
void loop2() {
while (true) {
// 执行一些操作
}
}
int main() {
// 创建两个线程来执行无限循环函数
std::thread t1(loop1);
std::thread t2(loop2);
// 主线程执行一些操作
while (true) {
// 执行一些操作
}
// 等待两个线程结束
t1.join();
t2.join();
return 0;
}
```
在上面的代码中,我们定义了两个无限循环函数`loop1`和`loop2`,然后在`main`函数中创建了两个线程`t1`和`t2`来执行这两个函数。同时,主线程也在执行自己的操作。最后,通过调用`join`函数等待两个线程结束,并返回0表示程序正常退出。
相关问题
用C++编写程序:编写两个函数,实现动态链表的建立和输出,主函数调用。
好的,下面是代码示例:
```c++
#include <iostream>
using namespace std;
// 定义链表结构体
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
// 建立链表
ListNode* createList() {
ListNode *head = new ListNode(0); // 创建头结点
ListNode *p = head; // 定义指针p,初始指向头结点
int n;
cout << "请输入链表长度:";
cin >> n;
cout << "请输入链表元素:";
for (int i = 0; i < n; i++) {
int val;
cin >> val;
ListNode *node = new ListNode(val); // 创建新节点
p->next = node; // 将新节点插入到链表尾部
p = node; // p指向新节点
}
return head->next; // 返回头结点的下一个节点,即链表的第一个节点
}
// 输出链表
void printList(ListNode *head) {
ListNode *p = head;
while (p != NULL) {
cout << p->val << " ";
p = p->next;
}
cout << endl;
}
int main() {
ListNode *head = createList(); // 建立链表
printList(head); // 输出链表
return 0;
}
```
主函数调用了两个函数,分别是 `createList` 和 `printList`。`createList` 函数用来建立链表,`printList` 函数用来输出链表。其中,`ListNode` 是链表的结构体,包含一个整型变量 `val` 和一个指针变量 `next`,`createList` 函数中的 `for` 循环用来读入链表元素,`printList` 函数中的 `while` 循环用来遍历链表并输出链表元素。
用一个函数让t的值每秒增加一同时能够运行其他函数,使用c++
可以使用多线程来实现这个功能。以下是一个示例代码:
```c++
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
void increaseT(int& t) {
while (true) {
this_thread::sleep_for(chrono::seconds(1)); // 每秒钟增加一次
t++;
}
}
void doOtherThings() {
while (true) {
cout << "I am doing other things." << endl;
this_thread::sleep_for(chrono::milliseconds(500)); // 每 0.5 秒进行一次其他操作
}
}
int main() {
int t = 0;
thread t1(increaseT, ref(t)); // 启动一个新的线程来执行 increaseT 函数
thread t2(doOtherThings); // 启动另一个线程来执行 doOtherThings 函数
while (true) {
cout << "t = " << t << endl;
this_thread::sleep_for(chrono::seconds(1)); // 每秒钟输出一次 t 的值
}
return 0;
}
```
在这个示例代码中,我们定义了两个函数 `increaseT` 和 `doOtherThings`,分别用于每秒钟增加变量 `t` 的值和进行其他操作。我们使用 `std::thread` 类来创建两个线程来执行这两个函数。同时,我们在主函数中也通过循环来输出变量 `t` 的值,以便观察 `increaseT` 函数是否能够正常运行。
在 `increaseT` 函数中,我们使用 `std::this_thread::sleep_for` 函数来让线程每秒钟暂停一次,然后将变量 `t` 的值加一。在 `doOtherThings` 函数中,我们也使用了 `std::this_thread::sleep_for` 函数来让线程每 0.5 秒暂停一次,然后输出一些信息。
需要注意的是,在主函数中,我们使用了一个无限循环来输出变量 `t` 的值。这个循环会一直运行,直到程序被终止。因此,我们需要在程序结束的时候手动终止它。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)