|
|
Home » Community » Newbie corner » Simple thread example
Simple thread example [message #29574] |
Mon, 01 November 2010 10:21  |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
I searched the Upp directory for "thread", nothing found unless I did something wrong. When I got back to an internet connection I searched the forum which found a considerable amount of posts. Some involved servers, OpenGL, etc..
All my app needs is to read a file line then Sleep(1000);
then repeat for all lines.
I am porting a java app (with an extra thread) that works OK in C++ except for the thread.
Without an extra thread this locks up the main thread.
A simple example of a thread with the C++ source and the upp file would be appreciated.
edit: MS Vista will be great to get away from.
Fired up my dependable XP and did a search for "thread" in the Upp directory and found 119. Vista found none.
[Updated on: Mon, 01 November 2010 11:44] Report message to a moderator
|
|
|
|
Re: Simple thread example [message #29582 is a reply to message #29577] |
Mon, 01 November 2010 22:08   |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
dolik.rce wrote on Mon, 01 November 2010 12:59 | Hi Neilson,
1. First a hint: you can search in package selection dialog and in help inside theide. It is usually faster (and as it seems even more reliable ) than windows file search 
Probably simplest possible example suiting your needs:#include <Core/Core.h>
using namespace Upp;
void ThreadFn(){
while(!Thread::IsShutdownThreads()){
Cout()<<"doing something here\n";
Sleep(1000);
}
}
CONSOLE_APP_MAIN{
Thread::Start(callback(ThreadFn));
Sleep(4000);
Thread::ShutdownThreads();
}
2. Note that you have to set MT flag to compile it (otherwise Thread is not defined).
If you need finer control of the thread, you can also use little bit different syntax: Thread t;
t.Run(callback(ThreadFn)); The variable t can than be used for example to Wait() for the thread to end or set its priority(win32 only).
Best regards,
Honza
|
Thanks Honza, that is a great help.
1. TheIde "Search" works good. Even Win XP will not search in Java files but Eclipse will.
2. Project->Main package configuration->right click->Append row->MT
The upp file now has:
mainconfig
"" = "GUI",
"" = "MT";
I will try working your code into my app.
Thanks (much)
Neil
edit: One thing I tinkered with in Python was to print each line of a file, stop it, and rewind (from that point read the previous lines.
I did not get that in the Java app but will in C++, it interacts with a display app that replays a track on a moving map.
I will include a link to the app in "Applications" on this forum when I get it to work.
[Updated on: Mon, 01 November 2010 22:25] Report message to a moderator
|
|
|
|
|
Re: Simple thread example [message #29674 is a reply to message #29672] |
Sat, 06 November 2010 20:03   |
|
nlneilson wrote on Sat, 06 November 2010 19:45 | Is there a way in Upp/C++ to close all open files and end all threads started by the app when clicking the red x to end?
|
I usually do this in the destructor of the class that contains the file stream or of the main window of my app. Possibly after IsOpen() test, if necessary.
Honza
|
|
|
Re: Simple thread example [message #29675 is a reply to message #29674] |
Sun, 07 November 2010 07:02  |
nlneilson
Messages: 644 Registered: January 2010 Location: U.S. California. Mojave &...
|
Contributor |
|
|
This seems to shutdown OK.
void Work(){ // this is the worker thread
...
while(!in.IsEof()){
if(Thread::IsShutdownThreads()){
in.Close();
break;
}
....
A few things still need to be done.
Thanks for the help.
Neil
|
|
|
Goto Forum:
Current Time: Sat May 10 00:34:44 CEST 2025
Total time taken to generate the page: 0.02782 seconds
|
|
|