Solved

Two threads cannot modify the same ITIMER_REAL under Debian Linux?

Posted on 2001-09-18
6
288 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
qos on linux 3 96
CENTOS7 DL380 G5  failed to install even after hpsa allowed 5 1,016
Problem to line 1 45
Replication of Nagios surveilance data 2 91
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 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

18 Experts available now in Live!

Get 1:1 Help Now