User Tools

Site Tools


in204:tds:sujets:td8:part3

This is an old revision of the document!


Exécution asynchrone

TD8

Références

Question n°1

Nous considérons la fonction suivante qui calcule les décimales de « e ».

std::string computeE(int numberOfDigits)
{
	int sizeOfTable = numberOfDigits + 9;
	int* table = (int*)_alloca(sizeOfTable * sizeof(numberOfDigits));
	table[0] = 0;
	table[1] = 2;
	for (int i = sizeOfTable - 1; i > 0; i--) {
		table[i] = 1;
	}
 
	std::ostringstream output;
	int x = 0;
	table[1] = 2;
	for (; sizeOfTable > 9; sizeOfTable -- ) 
	{
		for (int i = sizeOfTable - 1; i > 0; i--) 
		{
			table[i] = x % i;
			x = 10 * table[i - 1] + x / i;
		}
		output << x;
	}
	return output.str();
}

Implanter la fonction et vérifier que celle-ci fonctionne correctement.

Correction

Correction

#include<sstream>
#include<iostream>
 
std::string computeE(int numberOfDigits)
{
	int sizeOfTable = numberOfDigits + 9;
	int* table = (int*)_alloca(sizeOfTable * sizeof(numberOfDigits));
	table[0] = 0;
	table[1] = 2;
	for (int i = sizeOfTable - 1; i > 0; i--) {
		table[i] = 1;
	}
 
	std::ostringstream output;
	int x = 0;
	table[1] = 2;
	for (; sizeOfTable > 9; sizeOfTable--)
	{
		for (int i = sizeOfTable - 1; i > 0; i--)
		{
			table[i] = x % i;
			x = 10 * table[i - 1] + x / i;
		}
		output << x;
	}
	return output.str();
}
 
int main()
{
	std::string value = computeE(100);
	std::cout << "e with " << 100 << " decimals\n" << value << std::endl;
}

Question n°2

Nous constatons que calculer 10000 ou 20000 décimales de e, cela prend du temps. Nous souhaitons transformer cela en une fonction asynchrone à l’aide de la fonction std::async.

Ecrire le code transformant la précédente fonction en une fonction asynchrone en utilisant la fonction std::async.

Question n°3

Lancer deux calculs asynchrones, l’un calculant les 1000 premières décimales, l’autre les 10000 premières décimales et affichés les résultats dès que ceux-ci sont disponibles.

in204/tds/sujets/td8/part3.1604339089.txt.gz · Last modified: 2020/11/02 17:44 by bmonsuez