Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Simple Thread (simple thread example.)
Simple Thread [message #55023] Mon, 05 October 2020 16:16 Go to next message
lovmy is currently offline  lovmy
Messages: 44
Registered: November 2015
Member

Hi,

i try to create a simple thread in my application, this is a part of the code:

Processus::Processus() {}

Processus::~Processus() {}

void Processus::Start( String parametres )
{
	Thread().Run(THISBACK1(Boucle,parametres));
	while(Thread().GetCount());
}

void Processus::Boucle( String parametres )
{
	for (int i = 0; i < 10; ++i)
	{
		std::cout << i << ",";
	}
}


I have error:

F:\upp/uppsrc/Core/Callback.h (110): error: 'CLASSNAME' has not been declared

Can you help me ?
Re: Simple Thread [message #55032 is a reply to message #55023] Mon, 05 October 2020 18:12 Go to previous messageGo to next message
Klugier is currently offline  Klugier
Messages: 822
Registered: September 2012
Location: Poland, Kraków
Experienced Contributor
Hello,

It seems that you have missed "typedef Processus CLASSNAME;" (C++03) "using CLASSNAME =Processus" (c++11) declaration in your class. However, in c++11 world you don't need this just replace with lambda:
void Processus::Start( String parametres )
{
        Thread thr;
	thr.Run([=] { Boucle(parametres); }); // <- In previous implementation you delete thread model immediately...
	while(thr.GetCount()); // <- Not very nice synchronization mechanism :)
}

void Processus::Boucle( String parametres )
{
	for (int i = 0; i < 10; ++i)
	{
		Cout() << i << ","; // <- Cout() better in U++ world!
	}
}


My working test code below:
#include <Core/Core.h>

using namespace Upp;

class Processus {
public:
	void Start( String parametres )
	{
		Thread thr;
		thr.Run([=] { Boucle(parametres); }); // <- In previous implementation you delete thread model immediately...
		while(thr.GetCount()); // <- Not very nice synchronization mechanism :)
	}
	
	void Boucle( String parametres )
	{
		for (int i = 0; i < 10; ++i)
		{
			Cout() << i << ",";
		}
	}

};

CONSOLE_APP_MAIN
{
	Processus().Start("asda");
}


Klugier


Ultimate++ - one framework to rule them all.

[Updated on: Mon, 05 October 2020 18:12]

Report message to a moderator

Re: Simple Thread [message #55033 is a reply to message #55023] Mon, 05 October 2020 19:04 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 827
Registered: August 2007
Experienced Contributor
Klugier has put it nicely.

However, there is an even simpler way if you need async behaviour, using AsyncWork worker threads.

CONSOLE_APP_MAIN
{
	auto Boucle = [](String parametres) -> void
	{
		for (int i = 0; i < parametres.GetLength(); ++i)
		{
			Cout() << String(parametres[i], 1) << ",";
		}
	};
	
	Async(Boucle, "Hello world").Get();
}


Note that AsyncWork is the Upp interpretation and implementation of future/promise pattern and there are other ways to utilize it.

Best regards,
Oblivion


[Updated on: Mon, 05 October 2020 19:25]

Report message to a moderator

Re: Simple Thread [message #55040 is a reply to message #55033] Tue, 06 October 2020 08:15 Go to previous messageGo to next message
lovmy is currently offline  lovmy
Messages: 44
Registered: November 2015
Member

Hello !

Thank you for your help, i need to learn more on lambda expression...

Just a question, what's the différence between thread and CoWork ?

Have a nice day !
Re: Simple Thread [message #55042 is a reply to message #55040] Tue, 06 October 2020 09:33 Go to previous message
mirek is currently offline  mirek
Messages: 13036
Registered: November 2005
Ultimate Member
lovmy wrote on Tue, 06 October 2020 08:15
Hello !

Thank you for your help, i need to learn more on lambda expression...

Just a question, what's the différence between thread and CoWork ?

Have a nice day !


CoWork is basically an interface to thread pool. That is a pool of waiting threads that get used on demand - this saves you the costs of creating and exiting threads.

CoWork allows you to schedule any number of jobs to be processed, possibly in parallel and then wait for all of them to be done.

That said, for most of work, CoWork is now superseded by CoDo, which basically starts the same job, usually represented by lambda, on as many CPU cores as possible.

Long story short:

If you are after improving performance by parallelizing work, check CoDo first (most iterations are best done in CoDo).

If CoDo is not suitable, check CoWork (e.g. quick-sort cannot be implemented with CoDo nicely, but can be implemented with CoWork).

For some background tasks, consider Async.

If you are after something else entirely, like long running background tasks (e.g. a thread that backups data in your application periodically), use raw Thread.

Eh, that was not so short after all Smile

Mirek
Previous Topic: How to load your GridCtrl in background thread
Next Topic: Translation files
Goto Forum:
  


Current Time: Tue Jan 19 09:57:53 CET 2021

Total time taken to generate the page: 0.01692 seconds