Results 1 to 6 of 6
/********* 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 ...
- 04-30-2008 #1Just Joined!
- Join Date
- Mar 2008
- Posts
- 13
sleep( ) in multithreading
/********* 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, 5 );
TimerTask *p2 = new TimerTask( ptr2 , 2);
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......
BUT EXPECTED ABOVE RESULT IS NOT OBTAINED......HOW TO ACHEIVE THIS
- 04-30-2008 #2Linux Newbie
- Join Date
- Mar 2008
- Location
- Hyderabad
- Posts
- 109
What is happening is that you are not waiting for threads.
What your program does is create first thread as the thread sleeps the control transfer to main the after the second call p2->start() another thread is created that also sleeps again the control gets transfered to main.
Finally the main function exits. So both the threads are gone.
There is a function pthread_join to wait for the threads to complete. Or as in your case you want an infinite printing of messages just add.
while(1); after p2->start();
- 04-30-2008 #3Just Joined!
- Join Date
- Mar 2008
- Posts
- 13
Hi digvijay,
I have modified the code a bit.
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.
there should be context switching between the threads
- 04-30-2008 #4
Mugdha, would you be so kind as to post the most recent version of a complete program, which will compile without syntax errors and which illustrates the problem? Complete with all the #includes and everything? So we can compile it and play with it?
I won't be able to get to it for at least six hours (busy morning, sorry), but if you haven't solved it by then, I'll look at it.--
Bill
Old age and treachery will overcome youth and skill.
- 04-30-2008 #5Linux Newbie
- Join Date
- Mar 2008
- Location
- Hyderabad
- Posts
- 109
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 );
}
/********* main .cpp****************/
void *timer_task1(void *p)
{
for(int tick=0;tick<10;tick++)
{
printf("\nthread1\n");
sleep(m_period);
}
}
void *timer_task2(void *p)
{
for(int tick=0;tick<10;tick++)
{
printf("\nthread2\n");
sleep(m_period);
}
}
int main()
{
void *( *ptr1)( void *);
void *( *ptr2)( void *);
ptr1 = timer_task1;
ptr2 = timer_task2;
TimerTask *p1 = new TimerTask( ptr1, 5 );
TimerTask *p2 = new TimerTask( ptr2 , 5);
p1->start();
p2->start();
while(1);
}
Try it i m not on Linux today so can't test.
- 05-01-2008 #6
Mugdha, have you found the problem? If not, maybe you can post that complete, compilable, latest version that reproduces the problem so we can play with it. :)
--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote