User Tools

Site Tools


in204:tds:sujets:td8:part1

This is an old revision of the document!


Création & Manipulation de Processus léger

TD8

Question n°1

Créer un processus léger qui est associé à une fonction simple.

#include<iostream>
#include<thread>
 
void simple_method()
{
	int i = 5;
	int x = 10;
	int result = i * x;
	std::cout << "This code calculated the value "
		<< result << " from thread ID: " 
		<< std::this_thread::get_id() << "\n";
}
 
 
int main()
{
	std::thread simpleThread(&simple_method);
	std::cout << "Main thread is executing and waiting\n";
	simpleThread.join();
	std::cout << "Alternate thread has terminated.\n";
	return 0;
}

Exécuter le code et analyser la sortie. Commenter celle-ci, notamment au regard de la documentation de la classe std::tread.

Question n°2

Ecrire un programme qui lance les deux calculs suivants en parallèle, le premier dans un processus léger secondaire, le premier dans le processus léger principal :

void worker_process(int numberOfIterations)
{
	for (int i = 1; i < numberOfIterations; i++)
	{
		std::cout << "Worker Thread: " <<  i << "\n";
	}
}

et

void main_process()
{
	for (int i = 1; i < 1000; i++)
	{
		std::cout << "Primary Thread: " << i << "\n";
	}
}

Tester le code.  

in204/tds/sujets/td8/part1.1573486725.txt.gz · Last modified: 2019/11/11 15:38 by bmonsuez