我是初学者C ++程序员。我知道语句将逐行执行,所以如果我在main中调用一个函数,则调用函数体将首先执行,然后执行其余的代码......
如下面的代码所示:
int main()
{
ABC();
// ...
}
void ABC()
{
// ...
}
因此,当前程序的执行是同步,但我希望它是异步。
抱歉标题不好!我找不到更好的标题!如果你可以编辑它。
感谢您对像我这样的初学程序员的帮助:D
答案 0 :(得分:3)
如果你正在使用C ++ 11,你可以考虑使用std :: async:
否则,您可以轻松地:
创建一个帖子
设置计时器
答案 1 :(得分:2)
异步通常不适合初学者,但由于你想学习它,这里有一个如何做的例子。您只需复制代码并粘贴然后运行即可。研究代码。他们的评论非常好,让您理解。 看看哪一个最适合你。
方法1: 需要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 ++,我会提到您不应该使用&#34; 使用命名空间std &#34;因为它可能导致程序变大和其他命名冲突。
让我们解决下面问题:
#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;
}
方法2:
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
}
的详细信息
方法3:
C ++ 11不需要
POSIX标准 适用于 Windows,Mac和Linux
我们将在ABCD功能中从1 t0 10,000计数,而在主功能中计数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;
}
}
这里的问题是主要功能将首先完成计数,因为它主要功能计数到10,000时最多可计数5,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 ++线程初学者。一旦你理解了我提供的方法3 的基本知识,我建议你依靠semaphore。 有关pthread_t
的详细信息