Neutrino thread/photon window interaction

My first self-tutoring project in QNX 6.2.1 (Pro. eval. edition) is to
start a thread that generates a random number for display every N seconds.
The creation of the thread is a function associated to a “Init” button in
a window built under PhAB. Whether the new number is actually updated is
governed by a flag that is set or reset by another pair of
PhAB-constructed buttons.

The problem is that the thread seems to only execute once and the window
closes when a sleep(N) function is invoked for the planned delay. When
sleep(N)is commented out then the thread hammers the output area
continuously until I slay the whole task.

The thread for display widget “text_out”:

void* timed_data(void* arg) {
//omit some declarations…
do {
if (running) {
sprintf( buf, “%d”, rand() ) ;
PtSetResource( ABW_text_out, Pt_ARG_TEXT_STRING,
buf, 0) ;
}
sleep( 2 ) ; // or delay(2000);
} while ( 1 ) ;

The initiation function tied to button “init”:

int init_time_data( /usual args/ ) {
pthread_attr_t data_attr ;

pthread_attr_init( &data_attr ) ;
pthread_attr_setdetachedstate( &data_attr,
PTHREAD_CREATE_DETACHED );
pthread_create( NULL, &data_attr, &timed_data, NULL ) ;

return Pt_CONTINUE ;
}

Why doesn’t timed_data() run continuously with a 2-second pause between
widget updates?

Thanks for reading this far!

Bob

Usually, try set resources only in the mainthread.

Function for threadsafety are:
PtEnter() and PtLeave();

In your case you maybe need after the PtSetResource an PtFlush();

\

Mit freundlichem Gruß
Sascha Morgenstern

BitCtrl Systems GmbH
Weißenfelser Straße 67
Germany - 04229 Leipzig
Phon. +49 341 490 670
FAX. +49 341 490 67 15
eMail: info@bitctrl.de
WWW: http://www.bitctrl.de



“Bob Bottemiller” <robert.bottemiller@fmcti.com> schrieb im Newsbeitrag
news:c7avic$shg$1@inn.qnx.com

My first self-tutoring project in QNX 6.2.1 (Pro. eval. edition) is to
start a thread that generates a random number for display every N seconds.
The creation of the thread is a function associated to a “Init” button in
a window built under PhAB. Whether the new number is actually updated is
governed by a flag that is set or reset by another pair of
PhAB-constructed buttons.

The problem is that the thread seems to only execute once and the window
closes when a sleep(N) function is invoked for the planned delay. When
sleep(N)is commented out then the thread hammers the output area
continuously until I slay the whole task.

The thread for display widget “text_out”:

void* timed_data(void* arg) {
//omit some declarations…
do {
if (running) {
sprintf( buf, “%d”, rand() ) ;
PtSetResource( ABW_text_out, Pt_ARG_TEXT_STRING,
buf, 0) ;
}
sleep( 2 ) ; // or delay(2000);
} while ( 1 ) ;

The initiation function tied to button “init”:

int init_time_data( /usual args/ ) {
pthread_attr_t data_attr ;

pthread_attr_init( &data_attr ) ;
pthread_attr_setdetachedstate( &data_attr,
PTHREAD_CREATE_DETACHED );
pthread_create( NULL, &data_attr, &timed_data, NULL ) ;

return Pt_CONTINUE ;
}

Why doesn’t timed_data() run continuously with a 2-second pause between
widget updates?

Thanks for reading this far!

Bob

Hi Bob

Maybe I’m assuming something that isn’t true here. Are you aware that
photon programming is supposed to be event driver? You program
should not loop and sleep. All of the code that you write for a photon
program is executed as callbacks, i.e. they execute once and return some
kind of status.

If you want your program to wake up every N seconds, add a PtTimer
widget. Then write a callback to be executed every time the timer goes
off.

If I misunderstood your post and you are aware of this, I’m sorry.
This is an extreamly important point if you don’t have it yet.


Bob Bottemiller <robert.bottemiller@fmcti.com> wrote:

BB > My first self-tutoring project in QNX 6.2.1 (Pro. eval. edition) is to
BB > start a thread that generates a random number for display every N seconds.
BB > The creation of the thread is a function associated to a “Init” button in
BB > a window built under PhAB. Whether the new number is actually updated is
BB > governed by a flag that is set or reset by another pair of
BB > PhAB-constructed buttons.

BB > The problem is that the thread seems to only execute once and the window
BB > closes when a sleep(N) function is invoked for the planned delay. When
BB > sleep(N)is commented out then the thread hammers the output area
BB > continuously until I slay the whole task.

BB > The thread for display widget “text_out”:

BB > void* timed_data(void* arg) {
BB > //omit some declarations…
BB > do {
BB > if (running) {
BB > sprintf( buf, “%d”, rand() ) ;
BB > PtSetResource( ABW_text_out, Pt_ARG_TEXT_STRING,
BB > buf, 0) ;
BB > }
BB > sleep( 2 ) ; // or delay(2000);
BB > } while ( 1 ) ;

BB > The initiation function tied to button “init”:

BB > int init_time_data( /usual args/ ) {
BB > pthread_attr_t data_attr ;

BB > pthread_attr_init( &data_attr ) ;
BB > pthread_attr_setdetachedstate( &data_attr,
BB > PTHREAD_CREATE_DETACHED );
BB > pthread_create( NULL, &data_attr, &timed_data, NULL ) ;

BB > return Pt_CONTINUE ;
BB > }

BB > Why doesn’t timed_data() run continuously with a 2-second pause between
BB > widget updates?

BB > Thanks for reading this far!

BB > Bob