phtread_create while pthread_cond_timedwait in gdb



Hi,

I experience a strange behavior while using gdb to test my software.
As long as I dont run it with gdb it works fine. My program is
simple.

Short description:
My main method does create a thread A and waits at
pthread_cond_timedwait (timeout is 10 seconds).
Thread A is performing a short sleep of 1 second and creates a thread
B.
Thread B does a simple printf.

It seems that everytime I do create a thread 'pthread_cond_timedwait'
return with ETIMEDOUT instantly. I have already tried this with
semaphores and the function sem_timedwait() with the same result.


#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

static void *func1(void*);
static void *func2(void*);


static pthread_cond_t s_cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t s_mutex = PTHREAD_MUTEX_INITIALIZER;

static pthread_t s_thread1;
static pthread_t s_thread2;

int main(int argc, char **argv)
{
int rc = 0;
struct timespec curts = { 0 }; /* transformed timeout
value */

/* create thread */
if (pthread_create(&s_thread1, NULL, func1, NULL))
{
printf("Unable to create timer thread\n");
return 0;
}

if (clock_gettime(CLOCK_REALTIME, &curts) == -1)
{
printf("Could not read realtime timer \n");
return 0;
}

/* Add 10 seconds to current time*/
curts.tv_sec += 10;

if(pthread_mutex_lock(&s_mutex))
{
printf("Could not lock mutex \n");
return 0;
}

printf("THREAD 0: pthread_cond_timedwait\n");

rc = pthread_cond_timedwait(&s_cond, &s_mutex, &curts);
if (rc == ETIMEDOUT)
{
printf("THREAD 0: Timer expired \n");
}

if(pthread_mutex_unlock(&s_mutex))
{
printf("Could not unlock mutex \n");
return 0;
}

return 1;
}

/
*----------------------------------------------------------------------------
*/

static void *func1(void *args)
{
/* warte 1 sekunde */
printf("THREAD 1: usleep(1000000)\n");
usleep(1000000);

/* create thread */
if (pthread_create(&s_thread2, NULL, func2, NULL))
{
printf("Unable to create timer thread\n");
return NULL;
}

return NULL;
}

/
*----------------------------------------------------------------------------
*/

static void *func2(void *args)
{
printf("THREAD 2: printf\n");

return NULL;
}

When I run this program without gdb I get the following (as expected):

THREAD 0: pthread_cond_timedwait
THREAD 1: usleep(1000000)
THREAD 2: printf
/* waiting 9 seconds */
THREAD 0: Timer expired

When I run this program WITH gdb I get following:

[New Thread -1210066256 (LWP 13250)]
[New Thread -1210070112 (LWP 13253)]
THREAD 0: pthread_cond_timedwait
THREAD 1: usleep(1000000)
[New Thread -1218462816 (LWP 13254)]
[Thread -1210070112 (LWP 13253) exited]
THREAD 2: printf
[Thread -1218462816 (LWP 13254) exited]
/* doesnt wait single second */
THREAD 0: Waiting for timer timeout failed

Can anyone tell me why this only occurs when I start my program with
gdb?

thx in advance!
.



Relevant Pages

  • Re: FreeBSD on xen hvm
    ... Any chance it's possible to get a core for this, or attach GDB ... inspect the callout structure more easily. ... It just panics in the next thread created. ... It sounds like Xen may start the timer firing sooner than on plain ...
    (freebsd-current)
  • Re: FreeBSD on xen hvm
    ... Any chance it's possible to get a core for this, or attach GDB ... inspect the callout structure more easily. ... It sounds like Xen may start the timer firing sooner than on plain ...
    (freebsd-current)
  • Re: phtread_create while pthread_cond_timedwait in gdb
    ... When I run this program without gdb I get the following (as ... THREAD 2: printf ... THREAD 0: Timer expired ... static void *func1; ...
    (comp.os.linux.development.apps)
  • Re: [ANNOUNCE] 2.6.15-rc5-hrt2 - hrtimers based high resolution patches
    ... > And going into gdb, ... the above code would be very bad if this happens after preempting ... > the running timer. ... > softirq to finish, and then the softirq will wake up all the processes. ...
    (Linux-Kernel)
  • Re: phtread_create while pthread_cond_timedwait in gdb
    ... I experience a strange behavior while using gdb to test my software. ... Thread B does a simple printf. ... Waiting for timer timeout failed ... The above code doesn't feature string "THREAD 0: Waiting for timer ...
    (comp.os.linux.development.apps)