C++ 如何使此代码中的线程独立运行
我是线程概念的新手,我试图更好地理解它们。在阅读了关于线程的理论之后,我决定用线程编写一个简单的程序。我在互联网上发现了这个(可能很简单)任务: 编写一个计算素数和fibi数的程序 在不同的线程上 a。第一个线程从1开始搜索素数,直到 终止程序,并在找到素数时终止程序 必须提供查找此号码的时间 b。第二个线程是计算和打印FIBI的编号 从1一直到程序终止。当一个新的FIBI数 发现程序打印此数字和计算此数字的时间 号码 c。时间以毫秒(毫秒)为单位显示 d。想一想避免来自网络的信息重叠的策略 控制台 e。程序必须使用尽可能多的数字。当 数字太大,无法像无符号长字符那样保存在类型中 程序停止计算此类数字并显示错误 信息 示例输出: 质数1,0.1ms Fibuncai 1,0.1 ms 质数2,0.1毫秒 fibuncai 2,0.1ms 质数3,0.1毫秒 Fibuncai 3,0.1 ms Fibuncai 5,0.1 ms 质数5,0.1毫秒 Fibuncai 8,0.1 ms 质数7,0.1毫秒 Fibuncai 13,0.2 ms Fibbunaci 21,0.1 ms 质数11,0.2毫秒 这是我写的代码:C++ 如何使此代码中的线程独立运行,c++,multithreading,thread-synchronization,C++,Multithreading,Thread Synchronization,我是线程概念的新手,我试图更好地理解它们。在阅读了关于线程的理论之后,我决定用线程编写一个简单的程序。我在互联网上发现了这个(可能很简单)任务: 编写一个计算素数和fibi数的程序 在不同的线程上 a。第一个线程从1开始搜索素数,直到 终止程序,并在找到素数时终止程序 必须提供查找此号码的时间 b。第二个线程是计算和打印FIBI的编号 从1一直到程序终止。当一个新的FIBI数 发现程序打印此数字和计算此数字的时间 号码 c。时间以毫秒(毫秒)为单位显示 d。想一想避免来自网络的信息重叠的策略 控
#include <iostream> // std::cout
#include <string> // std::cout << std::string
#include <thread> // std::thread
#include <time.h> // clock()
#include <mutex> // std::mutex
std::mutex mtx;
int timePrim, timeFib;
bool isPrime(int testedNumber)
{
if (testedNumber <= 2) return true;
if (testedNumber % 2 == 0) return false;
for (int i = 3; (i*i) <= testedNumber; i += 2)
{
if (testedNumber % i == 0) return false;
}
return true;
}
// the function is realized by a recursive algorithm; credits to stackoverflow ;)
bool isFibonacci(unsigned long long testedNumber, int a = 1, int b = 1)
{
if (testedNumber == 0 || testedNumber == 1)
return true;//returning true for 0 and 1 right away.
int nextFib = a + b;//getting the next number in the sequence
if (nextFib > testedNumber)
return false;//if we have passed the tested number, it's not in the sequence
else if (nextFib == testedNumber)
return true;//if we have a perfect match, the tested number is in the sequence
else
isFibonacci(testedNumber, b, nextFib);//otherwise, get the next fibonacci number and repeat.
}
void CheckNumber(unsigned long long numberToCheck, bool ifPrime)
{
bool result = false;
if (ifPrime == true)
{
result = isPrime(numberToCheck);
}
else
{
result = isFibonacci(numberToCheck);
}
if (result == true)
{
float currentTime = 0;
std::string typeNumber = "";
if (ifPrime == true)
{
typeNumber = "Prime";
currentTime = (float)(clock() - timePrim) / CLOCKS_PER_SEC;
timePrim = clock();
}
else
{
typeNumber = "Fibonacci";
currentTime = (float)(clock() - timeFib) / CLOCKS_PER_SEC;
timeFib = clock();
}
mtx.lock();
std::cout << typeNumber << " number " << numberToCheck << "; time " << currentTime << std::endl;
mtx.unlock();
}
}
int main()
{
timePrim = timeFib = clock();
for (unsigned long long i = 0; true; i++) // endless for loop == while(true) // by requirements
{
std::thread primeThread = std::thread(CheckNumber, i, true);
std::thread fibThread = std::thread(CheckNumber, i, false);
primeThread.join();
fibThread.join();
}
}
#包括//std::cout
#include//std::cout创建std::thread
对象的两个向量。一个用于斐波那契计算,一个用于质数计算。然后有两个循环:一个用于创建两个线程并将它们添加到向量中,另一个循环用于连接向量中的所有线程
当第二个循环等待第一个线程退出时,在第一个循环中创建的所有线程将并行运行
现在要做的是创建两个线程,然后立即等待它们结束,然后再次在循环中迭代。这意味着一次只能运行一个Fibonacci和一个素数线程。现在您的程序为每个数i
创建线程,因此如果您计算素数和fibonaccis,其中i
是从0到gazilion,它将创建并销毁2个gazilion
线程。线程创建需要完成系统调用,在循环中这样做并不特别快
此外,还可以让两个线程在提交更多工作之前互相等待
以下是您的程序在伪代码中的外观(与真实编程语言的任何相似之处都纯属巧合):
相反,您可以为每个任务和循环分别创建2个永久线程:
def prime():
for i from 0 to gazilion:
calculate one prime
def fibo():
for i from 0 to gazilion:
calculate one fibo
prime_thread = create_and_run_thread(prime) # expensive, but runs only once
fibo_thread = create_and_run_thread(fibo) # expensive, but runs only once
wait_for(prime_thread) # you only wait until gazilion is reached
wait_for(fibo_thread) # you only wait until gazilion is reached
destroy_thread(prime_thread) # runs oly once
destroy_thread(fibo_thread) # runs oly once
你打算有多少线?2、3或无限?join()等待线程完成,因此每次循环都是序列化的
def prime():
for i from 0 to gazilion:
calculate one prime
def fibo():
for i from 0 to gazilion:
calculate one fibo
prime_thread = create_and_run_thread(prime) # expensive, but runs only once
fibo_thread = create_and_run_thread(fibo) # expensive, but runs only once
wait_for(prime_thread) # you only wait until gazilion is reached
wait_for(fibo_thread) # you only wait until gazilion is reached
destroy_thread(prime_thread) # runs oly once
destroy_thread(fibo_thread) # runs oly once