Solved

Two threads cannot modify the same ITIMER_REAL under Debian Linux?

Posted on 2001-09-18
6
285 Views
Last Modified: 2008-02-01
Hi,

I have a problem.  I have two threads and one sig handler that has to be called around every 100msec . I do this by setting the ITIMER_REAL with the setitimer. This handler is set by one of the two threads.But in a special situation, this signal should not arrive. The other thread notices this during execution and resets the ITIMER_REAL so that it does not deliver any signals any more.
But this does not work. It seems that two different signals are working separately to call the sig handler. Since if I say that the other thread should call the sig handler after 20 msec the sig handler is called twice (once after 20 and once after 100 msec.) So my question is:

How can I let one thread reset the timer for a ITIMER_REAL signal that is set by another thread......


I'm Running on Debian Linux 2.2.17, gcc version 2.95.2 with library -lpthread
0
Comment
Question by:Jelle
  • 2
  • 2
  • 2
6 Comments
 
LVL 5

Expert Comment

by:garboua
Comment Utility
can U post the code where you actually launch the threads, it would be helpful.
0
 

Author Comment

by:Jelle
Comment Utility
In the main the following command is called:
  pthread_t sense;
  SenseHandler s;
  pthread_create( &sense, NULL, sense_callback  , &s);

/**** THREAD 1 ******/
sense_callback looks as follows (argument s to let thread call a method from a class)

void* sense_callback( void *v )
{
  Log.log( 1, "Starting to listen for server messages" );
  SenseHandler* s = (SenseHandler*)v;
  s->handleMessagesFromServer( );
  return NULL;
}

handleMessagesFromServer loops infinite and does the following (among other things):
  struct sigaction sigact;

  sigact.sa_flags = SA_RESTART; // make sure primitives (recvfrom) are not unblocked.
  sigact.sa_handler = (void (*)(int))sigalarmHandler;
  sigaction( SIGALRM, &sigact, NULL );

  itv.it_interval.tv_sec = 0;
  itv.it_interval.tv_usec = 0;
  itv.it_value.tv_sec = 0;

  while( 1 )
  {
    receiveMessage(); // from a socket connection
    .. handle message and determine when signal has to arrive
    itv.it_value.tv_usec = iTimeSignal;
    setitimer( ITIMER_REAL, &itv, NULL );
  }

/**** MAIN THREAD ******/
Main thread does other actions but sometimes want to reset the timer set by thread 1. I do that like this.

  struct itimerval itv2;
  itv2.it_interval.tv_sec = 0;
  itv2.it_interval.tv_usec = 0;
  itv2.it_value.tv_sec = 0;
  itv2.it_value.tv_usec = 0;

  setitimer( ITIMER_REAL, &itv2, NULL );

Strange thing is that this not work. But if I change itv2.it_value.tv_usec to 20*1000, the sigalarmHandler is called twice as many times (so after the signal set by thread 1 and after the signal by the main thread). I like the main thread to destroy the timer that was set was by thread 1. But I don't know how to that....
0
 
LVL 5

Accepted Solution

by:
bryanh earned 100 total points
Comment Utility
>It seems that two different signals are working
>separately to call the sig handler.

Since a signal is an event, or a notification of that event, this statement doesn't make sense.  However, I think what you observed is that two different timers are separately generating signals to their own threads.

That's true.  Linux pthreads don't share timers.  In fact, they don't share much at all -- mainly just memory.

A Linux pthread is a process, and a process cannot modify another process' timer.

But it's hard to imagine how you need this function.  Main Thread could just set a variable to tell Thread 1's signal handler to ignore the signal and/or cancel the timer.  Main Thread could also signal Thread 1 so Thread 1 can cancel its timer immediately.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Jelle
Comment Utility
Thanks, I thought I read somewhere (man pages) that threads under Linux consisted as one process, so I didn't understand why the process' timer hadn't had any effect. I solved it using the variable you mentioned and it seems to work now. Thanks for the help..

Jelle

Statement about two different signals is of course nonsense ;-)
0
 
LVL 5

Expert Comment

by:bryanh
Comment Utility
You may have confused pthreads with other kinds of threads.  There are thread libraries (one is called Linuxthreads or something like that) that create threads within one process, without the kernel even knowing about them.  Before pthreads (before Linux 2.2, I think) that was the only way.

I've also heard of other pthreads implementations (not Linux), where pthreads exist within one process until one of them wants to block the process, at which time the process spawns a child to do the waiting.  Linux is nowhere near that complicated.

I'm not an expert on the comparative advantages of threading strategies, but I've heard Linux's thread=process criticized as inefficient.  (I've also heard criticisms that it clutters a 'ps' listing.  Sometimes there are hundreds of threads).
0
 
LVL 5

Expert Comment

by:garboua
Comment Utility
sorry could not get  online for a while, my apology, bryanh did the job though.  if you would like a time to share between all your threads you can download one from sourceforge.  there are plenty timers fully implemented "C++" where you can start the timer and pass class pointer to your threads, and it is easy to interogate the timers.  
www.sourceforge.com and look at snipp code under C++
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now