help: why dosent this code work?
vesper_carmen_at_yahoo.com
Date: 02/20/05
- Next message: Peter T. Breuer: "Re: help: why dosent this code work?"
- Previous message: Mark Hobley: "Re: Line Conditioner and UPS?"
- Next in thread: Peter T. Breuer: "Re: help: why dosent this code work?"
- Reply: Peter T. Breuer: "Re: help: why dosent this code work?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 19 Feb 2005 23:41:26 -0800
Hi,
This code is supposed to print "hello world" character by character
with an inteval of 40 ms between it. But it goes into some kind of
infinite loop :(
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdio.h>
int delaycounter = 40;
//long delay = 40000;
//variables required for the program
char* h ="Hello World\n";
int t = 0;
//timers
struct timespec delay;
struct itimerval tval;
//mutex to implement critical sections
pthread_mutex_t lock;
// variables needed for timer
static sem_t sem; // semaphore that's signaled if timer
signal arrived
static int stopped = 0; // non-zero if the timer is to be
stopped
static pthread_t timer_thr; // thread in which user timer functions
execute
static void
timersignalhandler()
{
/* called in signal handler context, we can only call
* async-signal-safe functions now!
*/
sem_post(&sem); // the only async-signal-safe function pthreads
defines
}
static void *timerthread(void *_)
{
while (!stopped) {
sem_wait(&sem);
timerfunc(); // user-specific timerfunc, can do
anything it wants
}
return 0;
}
void
init_timer(long delay)
{
struct itimerval tval = { /* subsequent firings */ { 0, delay
},
/* first firing */ { 0, delay
}};
sem_init(&sem, /*not shared*/ 0, /*initial value*/0);
pthread_mutex_init(&lock, (pthread_mutexattr_t*)0);
pthread_create(&timer_thr, (pthread_attr_t*)0, timerthread,
(void*)0);
signal(SIGALRM, timersignalhandler);
setitimer(ITIMER_REAL, &tval, (struct itimerval*)0);
}
void
shutdown_timer()
{
stopped = 1;
// sem_post(&sem);
// pthread_join(timer_thr, 0);
}
void* print_thread(void*_)
{
pthread_mutex_lock(&lock);
while(!t);
t=0;
printf("\n %c", *h);
pthread_mutex_unlock(&lock);
shutdown_timer();
}
int timerfunc()
{
int ret;
//initialize the mutex
pthread_t printthread;
ret = pthread_create(&printthread, (pthread_attr_t*)0, print_thread,
(void*)0);
pthread_mutex_lock(&lock);
printf("\n in timerfunc critical section\n");
t=1;
pthread_mutex_unlock(&lock);
}
int main()
{
printf("\n In main \n");
//initialize mutex here?
init_timer(40000);
while(*h++)
{
while(!stopped);
stopped = 1;
init_timer(40000);
}
printf("\n %d \n", &delaycounter);
//sleep();
}
- Next message: Peter T. Breuer: "Re: help: why dosent this code work?"
- Previous message: Mark Hobley: "Re: Line Conditioner and UPS?"
- Next in thread: Peter T. Breuer: "Re: help: why dosent this code work?"
- Reply: Peter T. Breuer: "Re: help: why dosent this code work?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|