LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-08-2010, 10:53 PM   #1
manohar
Member
 
Registered: Dec 2010
Posts: 42

Rep: Reputation: 2
Timer issue in multi threading


[R]Hi,

Iam using timers in posix multithreading environment also facing the problem. When ever timer expires control is coming out of the program. Can some one plz help me.
My code is looks like


#include <pthread.h>
#include <signal.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef void (timer_callback) (union sigval);
/*
* Timer initialization function
*/
int timer_initialize(timer_t * timer_id, int period, timer_callback * cbf, void* data)
{
int status =0;
struct itimerspec ts;
struct sigevent se;

/* register callback func */
se.sigev_notify = SIGEV_THREAD;
se.sigev_value.sival_ptr = data;
se.sigev_notify_function = cbf;
se.sigev_notify_attributes = NULL;

if(timer_create(CLOCK_REALTIME, &se, timer_id) != 0)
{
printf(" error in creating a timer ");
exit(1);
}

/* assign the period */
ts.it_value.tv_sec = period;
ts.it_value.tv_nsec = 0;

if(timer_settime(*timer_id, 0, &ts, 0) != 0)
{
printf(" error in setting timer ");
exit(1);
}

return 0;
}
/*
* scanTimer callback function
*/
void scanTimer_callback(union sigval si)
{
char* evt = (int*) si.sival_ptr;
printf("\n Iam in [%s] callback \n", evt);
}

/*
* tileTimer callback function
*/
void tileTimer_callback(union sigval si)
{
char* evt = (int*) si.sival_ptr;
printf("\n Iam in [%s] callback \n", evt);
}
int main(int argc, char ** argv)
{

timer_t timer_ID;
timer_initialize(&timer_ID, 1, scanTimer_callback, (void *) "Scan" );
timer_initialize(&timer_ID, 2, tileTimer_callback, (void *) "Tile" );
getchar();

return 0;
}
[/B]

Last edited by manohar; 12-08-2010 at 11:14 PM.
 
Old 12-10-2010, 02:56 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
I'm not sure what you mean by 'control coming out of the program' when the timer expires. What output do you get?

You do need to set the it_interval component of the itimerspec structure. If you want the timer to keep firing, then set it to the same value as the it_value component; if you only want it to fire once, then set it to zero.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi threading in C pramod.srk Programming 2 03-01-2010 01:51 AM
Multi threading greplinux Linux - Newbie 2 12-29-2008 04:25 PM
Multi-threading in C Mr Tummyfluff Programming 3 11-08-2008 01:13 PM
multi threading slower than single threading on dual core. why? nebojsa.andjelkovic Programming 13 01-30-2007 09:56 PM
problems about multi-threading and/or multi-processing with tcp network in java ... alred Programming 5 06-23-2006 03:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration