help: why dosent this code work?

vesper_carmen_at_yahoo.com
Date: 02/20/05


Date: 19 Feb 2005 23:41:58 -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();
        
}



Relevant Pages

  • help: urgent why dosent this code work?
    ... This code is supposed to print "hello world" character by character ... struct timespec delay; ... static void *timerthread ...
    (comp.os.linux.development.apps)
  • help urgent: why dosent this code work?
    ... This code is supposed to print "hello world" character by character ... struct timespec delay; ... static void *timerthread ...
    (comp.os.linux.development.system)
  • help: why dosent this code work?
    ... This code is supposed to print "hello world" character by character ... struct timespec delay; ... static void *timerthread ...
    (comp.os.linux.misc)
  • Re: BFS vs. mainline scheduler benchmarks and measurements
    ... The max delay between this children is logged for each ... Vary the number of clients to ... static unsigned int min_delay = 100; ... static void log_delay ...
    (Linux-Kernel)
  • ATmega8535 and USART post-initialisation delay required?
    ... Is there a requirement to delay using the USART (at least for asynchronous ... enabling interupts until transmitting the first character? ... character, ... The ATmega8535 datasheet does not seem to say that a delay is required. ...
    (comp.arch.embedded)