Hi Simon,
I read your short description of the task. I understood that the thread you
want to suspend can be either working with outputs or “sleeping”. I don’t
know your configuration, but I guess it is not good idea to interrupt the
thread while it is working with hardware. I also don’t know how long does it
take to modify outputs, but again I guess that the thread spends more time
sleeping. Below is the one of the solutions. Actually, it is just an
illustration of what Robert Krten said here earlier.
Sincerely,
Serge
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/select.h>
#include <sys/neutrino.h>
#define TIMER_PULSE 0
static int chid;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static void *thread( void *data )
{
struct _pulse pulse;
int rcvid, fd = 1;
while( 1 ) {
/*
- The thread is “sleeping” here until timer pulse arrives
*/
if( -1 != ( rcvid = MsgReceive( chid, &pulse, sizeof( struct
_pulse ), NULL ))) {
switch( pulse.code ) {
case TIMER_PULSE:
/*
- Writing digital output or whatever…
- Timer also can be modified here.
*/
pthread_mutex_lock( &mutex );
write( fd, data, strlen( data ));
pthread_mutex_unlock( &mutex );
break;
default:
if( rcvid )
MsgReply( rcvid, ENOTSUP, NULL, 0 );
break;
}
}
}
return NULL;
}
int main( int argc, char **argv )
{
struct sigevent event;
int sec;
struct itimerspec timer;
char buff[ 1024 ];
int coid, timid;
if( -1 == ( chid = ChannelCreate( _NTO_CHF_DISCONNECT |
_NTO_CHF_UNBLOCK ))) {
perror( “ChannelCreate()” );
return EXIT_FAILURE;
}
if( -1 == ( coid = ConnectAttach( 0, 0, chid, _NTO_SIDE_CHANNEL, 0 )))
{
perror( “ConnectAttach()” );
return EXIT_FAILURE;
}
event.sigev_notify = SIGEV_PULSE;
event.sigev_coid = coid;
event.sigev_code = TIMER_PULSE;
event.sigev_priority = -1;
if( -1 == timer_create( CLOCK_REALTIME, &event, &timid )) {
perror( “timer_create()” );
return EXIT_FAILURE;
}
if( 0 != pthread_create( NULL, NULL, thread, buff )) {
perror( “pthread_create()” );
return EXIT_FAILURE;
}
for( sec = 1; ; sec++ ) {
pthread_mutex_lock( &mutex );
/*
- stop timer
*/
nsec2timespec( &timer.it_value, 0 );
timer_settime( timid, 0, &timer, NULL );
/*
- change output parameters
*/
sprintf( buff, “time interval: %ds\n”, sec );
/*
- change timer settings
*/
timer.it_value.tv_sec = sec;
timer.it_value.tv_nsec = 0;
memcpy( &timer.it_interval, &timer.it_value, sizeof( struct
timespec ));
timer_settime( timid, 0, &timer, NULL );
pthread_mutex_unlock( &mutex );
/*
- simulate some asyncronious behavior
*/
sleep( 2 * sec + 1 );
}
return EXIT_SUCCESS;
}
“Simon A. Platten” <simon.platten@vtcontrols.co.uk> wrote in message
news:arkp8d$ep3$1@inn.qnx.com…
Hi,
I agree with your comments, I am using pthread_cancel() to abort a thread
that is performing a specific function. I am reasonably new to QNX. The
problem I have is this.
I issue a command to a process, that process creates a thread to perform
the
command, which involves energising a digital output, going to sleep for a
short time then de-energising the output. The sleep time is variable.
If another command is issued to the process before the initial command has
completed then the initial command should be aborted and the new command
started.
This is why I use pthread_cancel() to abort the command. If there is a
cleaner way of aborting the thread whilst it is a sleep I’d like to hear
it.
Thank you,
Simon