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 example
Simple thread example [message #29574] Mon, 01 November 2010 10:21 Go to next message
nlneilson is currently offline  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 #29577 is a reply to message #29574] Mon, 01 November 2010 12:59 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi Neilson,

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 Smile ) than windows file search Wink

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();
}
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

[Updated on: Mon, 01 November 2010 13:02]

Report message to a moderator

Re: Simple thread example [message #29582 is a reply to message #29577] Mon, 01 November 2010 22:08 Go to previous messageGo to next message
nlneilson is currently offline  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 Smile ) than windows file search Wink

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 #29631 is a reply to message #29582] Thu, 04 November 2010 23:03 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
After a bunch of hours (plus updating to MSC10 and U++ 2827) got the thread to work.

	Thread work;

// At the end of file chooser to read the lines in the thread:
    	work.Run(THISBACK(Work));

    void Work(){
        ...
	while(!in.IsEof()){
	   ...



upp/Reference/GuiLock was something I could open and run.

Thanks for the help Honza!

Neil
Re: Simple thread example [message #29672 is a reply to message #29631] Sat, 06 November 2010 19:45 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
I have the Java app ported to C++

It works great! A little faster and less CPU usage than Java and no problems with heap space.

It reads a file with lines like this:
34.212518,-119.192363,36.0,N10801,113

It interacts through a socket with NASA WWJ as 3D tracking on the Earth WGS84 ellipsoid rather than a round globe or a flat space.

The fourth comma delimited value is an ID.
With a file with more positions/IDs can track/draw paths for up to 50 on a single core (<50% CPU usage) updated each second.

I do have one glitch in the C++ code.
In the Exit() function:
    	in.Close();
    	Thread::ShutdownThreads();

plus saving a config file with settings.
That works OK.

But clicking the red x to close the app gives errors.
Sometimes:
Assertion failed in C:\upp\uppsrc\Core\String.cpp, line 38 rc->rfcount>0

or just closes with these error details":
Problem signature:
  Problem Event Name:	APPCRASH
  Application Name:	Replay.exe
  Application Version:	0.0.0.0
  Application Timestamp:	4cd51eb8
  Fault Module Name:	Replay.exe
  Fault Module Version:	0.0.0.0
  Fault Module Timestamp:	4cd51eb8
  Exception Code:	80000003
  Exception Offset:	0013ad26
  OS Version:	6.0.6002.2.2.0.768.3
  Locale ID:	1033
  Additional Information 1:	0245
  Additional Information 2:	6ea91fa736a6c7dd9ccfae9a5d57ed32
  Additional Information 3:	fa73
  Additional Information 4:	93d639f2a4427fa651c7717b5f37f438


In Java had a similar problem. After a considerable amount of searching and tinkering
				af.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
closes all processes/threads associated with the app, otherwise a bunch of javaw processes need to be closed with task manager.

I would rather not tinker with the Upp core code. 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?


Re: Simple thread example [message #29674 is a reply to message #29672] Sat, 06 November 2010 20:03 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

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 Go to previous message
nlneilson is currently offline  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
Previous Topic: Upp (Help) documentation
Next Topic: Few questions
Goto Forum:
  


Current Time: Sat Apr 27 08:09:47 CEST 2024

Total time taken to generate the page: 0.91386 seconds