Home » Community » Newbie corner » Simple Thread (simple thread example.)
Simple Thread [message #55023] |
Mon, 05 October 2020 16:16  |
|
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   |
 |
Klugier
Messages: 1099 Registered: September 2012 Location: Poland, Kraków
|
Senior 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
U++ - one framework to rule them all.
[Updated on: Mon, 05 October 2020 18:12] Report message to a moderator
|
|
|
|
Re: Simple Thread [message #55040 is a reply to message #55033] |
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 !
|
|
|
Re: Simple Thread [message #55042 is a reply to message #55040] |
Tue, 06 October 2020 09:33  |
 |
mirek
Messages: 14255 Registered: November 2005
|
Ultimate Member |
|
|
lovmy wrote on Tue, 06 October 2020 08:15Hello !
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 
Mirek
|
|
|
Goto Forum:
Current Time: Fri Apr 25 15:10:56 CEST 2025
Total time taken to generate the page: 0.02603 seconds
|