LinuxQuestions.org
Register a domain and help support LQ
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
 
LinkBack Search this Thread
Old 04-30-2008, 07:09 AM   #1
mugdha
LQ Newbie
 
Registered: Apr 2008
Posts: 9

Rep: Reputation: 0
Timer Threads


/********* timertask.h***********/

class TimerTask
{
pthread_t m_timer1;
int m_period;
void *( *m_funcptr)( void *);
}

/*******timertask.cpp ************/

TimerTask :: TimerTask( void *( *functionPtr)( void *), int period )
{
m_funcptr = functionPtr;
m_period = period;

}

void TimerTask::start(){


pthread_create( &m_timer1, NULL, m_funcptr ,NULL );
for (int tick = 0; tick < 10; tick++)
{

sleep( m_period );
m_funcptr( NULL ); // call the thread function

}

}

/********* main .cpp****************/

void *timer_task1(void *p)
{

printf("\nthread1\n");

}

void *timer_task2(void *p)
{

printf("\nthread2\n");

}
int main()

{
void *( *ptr1)( void *);
void *( *ptr2)( void *);
ptr1 = timer_task1;
ptr2 = timer_task2;

TimerTask *p1 = new TimerTask( ptr1, 2 );
TimerTask *p2 = new TimerTask( ptr2 , 5);

p1->start();
p2->start();

}

/************************************************** ********************/
the output is
thread1
thread1
....... is printed for some time
then
thread2
thread2
thread2......is printed for some time

but the desired output is

thread1
thread2
thread1
thread2....or equivalent to that depending on the time interval passed.

my requirement is that when start() for p1 is called a thread is created and "thread1" is printed. but when sleep(10) is called control should go to new timertask i.e p2->start(). and again a new thread should be created which will print "thread2".
so output should be

"thread1"
"thread2"
"thread1"
"thread2
.
.
.
and so on......the sequence might change
BUT EXPECTED ABOVE RESULT IS NOT OBTAINED......HOW TO ACHEIVE THIS

Last edited by mugdha; 04-30-2008 at 09:14 AM.
 
Old 04-30-2008, 07:28 AM   #2
sundialsvcs
Senior Member
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 3,685

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
When you launch two threads, they're going to run independently from you ... and if the two threads need to interleave their operations they must use some kind of mutual-exclusion mechanism to do that.

When you launch two threads in a row, you do not know at what exact moment they will "launch." Your process might or might not give-up control; the new threads may or may not launch on the same CPU. The threads might start execution in a different order.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to create Timer based threads mugdha Programming 5 04-18-2008 08:16 AM
How can I get LAPIC timer to run instead of the PIT timer? sixerjman Linux - Kernel 1 10-16-2007 09:59 PM
Multimedia timer (SMP friendly timer) bigqueso Linux - Kernel 0 03-15-2007 03:49 PM
Creating a milisecond timeout timer for threads cardias Programming 5 08-05-2004 07:18 AM
Problems creating a milisecond timeout timer for threads cardias Linux - Software 1 08-04-2004 03:10 PM


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

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration