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!
.