Solved

Two threads cannot modify the same ITIMER_REAL under Debian Linux?

Posted on 2001-09-18
6
294 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
ID: 6493057
can U post the code where you actually launch the threads, it would be helpful.
0
 

Author Comment

by:Jelle
ID: 6493109
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
ID: 6502751
>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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Jelle
ID: 6503833
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
ID: 6503865
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
ID: 6508782
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
what is Amazon Linux AMI 6 69
Automating a set of commands run on linux 7 100
looking for a CENTOS ISO to download with x window installed 2 51
linux 13 77
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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