我是一名初学者 C++ 程序员。据我所知,语句将逐行执行,因此如果我在 main 中调用一个函数,被调用函数的主体将首先执行,然后再执行其余代码...
如下代码所示:
int main()
{
ABC();
// ...
}
void ABC()
{
// ...
}
所以我当前程序的执行是同步,但我希望它是异步。
抱歉标题不好!我找不到更好的问题标题!如果你可以请编辑它。
感谢您对像我这样的初学者程序员的帮助:D
最佳答案
提供示例而不是让初学者阅读长篇文章。所提供的文章或链接可能会随时下线,从而导致人们再次提出相同的问题。
Async 通常不适合初学者,但既然你想学习它,这里有一个如何做的例子。您可以只复制代码并粘贴然后运行它。研究代码。他们的评论非常好,您可以理解。 看看哪一个最适合您。
方法一: 需要 C++ 11
应适用于 Windows、Mac 和 Linux*
#include <iostream>
#include <future> //Import the asynchronous Library
using namespace std;
//The function you want to call asynchronously
void ABC()
{
cout<<"Hello From ABC Function"<<endl;
}
int main()
{
/*Setup **ABC function**, We will call it **MyABCFunction**, but you can call it anything else.
Inside MyABCFunction, we call **sync function** and pass it the name of the **function** we want
to call which is "ABC()". You don't include the "()", just the name "ABC".*/
future<void> MyABCFunction(async(ABC));
//Call ABC function
MyABCFunction.get();
//Show message from the Main Function
cout << "Hello From Main Function." <<endl;
return 0;
}
由于您是 C++ 的新手,我会提到您不应该使用“using namespace std”,因为它会导致程序变得更大和其他命名冲突。
让我们在下面修复它:
#include <iostream>
#include <future> //Import the asynchronous Library
/*No "using namespace std". Instead, each c++ library function must be begin with "std::"
which includes Standard library for the function needed
*/
//The function you want to call asynchronously
void ABC()
{
//std:: before cout and endl
std::cout<<"Hello From ABC Function"<<std::endl;
}
int main()
{
//std:: before future and async
std::future<void> MyABCFunction(std::async(ABC));
//Call ABC function
MyABCFunction.get();
//std:: before cout and endl
std::cout << "Hello From Main Function." <<std::endl;
return 0;
}
方法二:
不需要 C++ 11
仅适用于 Windows(最简单和最短的方法)
#include <iostream> //For text on screen
#include <windows.h> //Must include the use HANDLE class
#include <process.h> // needed for _beginthread()
//Function prototype of ABCD function to be called in a thread.
void ABCD(void *param);
int main()
{
int val = 0;
HANDLE handle; //Create a handle (Only on Windows OS)
/*Initialize the handle and begin thread. */
/*_beginthread takes the name of the function to be called "ABCD" */
/*_beginthread takes the stack size of the thread 0*/
/*_beginthread takes the parameter to be passed to the "ABCD" which is 0(val) since void ABCD(void *param) takes void which means no parameter*/
handle = (HANDLE) _beginthread( ABCD,0,&val);
/*Do infinite loop on the main function to prove that main and ABCD function are running at the same time*/
while(1)
{
std::cout<<"thread from main function"<<std::endl;
}
//Wait for ACBD to finish before exiting the program
WaitForSingleObject(handle,INFINITE);
return 0;
}
//ABCD function to be called from the thread
void ABCD(void *param)
{
/*Do infinite loop on the main function to prove that ABCD and main function are running at the same time*/
while(1)
{
std::cout<<"thread from ABCD function"<<std::endl;
}
_endthread(); // End thread. Won't be called since we are looping forever in while(1) loop
}
有关_beginthread的更多信息
方法三:
不需要 C++ 11
POSIX 标准 适用于 Windows、Mac 和 Linux
我们将在 ABCD 函数中从 1 到 10,000 计数,同时在 main 函数中从 1 计数到 5,000。
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
void *ABCD(void *arg)
{
//count from 1 to 10000 from the ABCD function thread
for(int i=1; i<=10000;i++)
{
std::cout<<"thread from ABCD function "<<i<<std::endl;
}
}
int main()
{
pthread_t myPthread;
//Create and call the ABCD function to start counting
pthread_create(&myPthread, NULL, ABCD, NULL);
//count from 1 to 5,000 from the main function thread
for(int i=1; i<=5000;i++){
std::cout<<"thread from main function"<<std::endl;
}
}
这里的问题是 main 函数会先完成计数,因为它计数到 5,000,而 main 函数计数到 10,000。当 main 函数先完成时,即使 ABCD 函数没有完成计数,它也会终止整个程序。 为了解决这个问题,我们使用pthread_join函数来等待ABCD函数完成,然后我们的程序才能终止。
完整代码如下:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
void *ABCD(void *arg)
{
//count from 1 to 10000 on from ABCD function thread
for(int i=1; i<=10000; i++)
{
std::cout<<"thread from ABCD function "<<i<<std::endl;
}
}
int main()
{
pthread_t myPthread;
//Create and call the ABCD function to start counting
pthread_create(&myPthread, NULL, ABCD, NULL);
//count from 1 to 5,000 on from the main function thread
for(int i=1; i<=5000; i++)
{
std::cout<<"thread from main function"<<std::endl;
}
//Wait for ABCD function to finish before we exit
int a =0;
pthread_join(myPthread, (void **)&a);
}
我希望这可以帮助所有 c++ 线程初学者。我建议你靠一下semaphore一旦您了解了我提供的方法 3 的基础知识。 有关 pthread_t 的更多信息
关于c++ - 我如何在 C++ 中使用异步函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27409901/