delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/06/08/12:20:06

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-ID: <009b01c56c45$e36b9930$1f3ca8c0@AlohaSunset.com>
From: "Mark Pizzolato" <list-cygwin AT subscriptions DOT pizzolato DOT net>
To: <cygwin AT cygwin DOT com>
Subject: gdb threading troubles
Date: Wed, 8 Jun 2005 09:19:33 -0700
MIME-Version: 1.0
X-IsSubscribed: yes

------=_NextPart_000_0098_01C56C0B.2EE7F240
Content-Type: text/plain;
	format=flowed;
	charset="iso-8859-1";
	reply-type=original
Content-Transfer-Encoding: 7bit

The attached example test program runs to completion when run directly, but 
spins infinitely when run under gdb.

I'm compiling with:

gcc -g -O0 mutexttest.c -o mutexttest

running under:
    cygwin    1.5.17-1
    gdb         20041228-3 

------=_NextPart_000_0098_01C56C0B.2EE7F240
Content-Type: text/plain;
	format=flowed;
	name="mutextest.c";
	reply-type=original
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="mutextest.c"

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdarg.h>

pthread_mutex_t log_mutex =3D PTHREAD_MUTEX_INITIALIZER;

void
logit(const char *fmt, ...) {
	va_list args;
	char buf[1024];
	int bytes;
=09
	buf[sizeof(buf)-1] =3D '\0';
	va_start(args, fmt);
	bytes =3D vsnprintf(buf, sizeof(buf)-1, fmt, args);
	va_end(args);
	pthread_mutex_lock(&log_mutex);
	printf("%s", buf);
	pthread_mutex_unlock(&log_mutex);
}

pthread_mutex_t mutex =3D PTHREAD_MUTEX_INITIALIZER;

struct TestInfo {
	void (*Lock)(void);
	void (*Unlock)(void);
	int Iterations;
	int Data;
	int Increment;
};

void
NoLock(void) {
}

void
NoUnlock(void) {
}

void
PthreadLock(void) {
	pthread_mutex_lock(&mutex);
}

void
PthreadUnlock(void) {
	pthread_mutex_unlock(&mutex);
}

void *
TestThread (void *arg) {
	struct TestInfo *t =3D (struct TestInfo *)arg;
	int i;
	int *pData =3D &t->Data;
=09
	logit("Thread %d starting...\n", pthread_self());
	sleep(1);
	for (i=3D0; i<t->Iterations; ++i) {
		int tmp;
		int trand;
		t->Lock();
		srand(46);
		trand =3D rand();
		srand(46);
		tmp =3D t->Data;
		tmp =3D tmp + trand - rand();
		tmp =3D tmp + t->Increment;
		t->Data =3D tmp;
		t->Unlock();
	}
	logit("Thread %d done.\n", pthread_self());
	return NULL;
}

main (int argc, char ** argv) {
	int threadcount =3D 10;
	pthread_t tid[10];
	int i;
	struct TestInfo Info;

	Info.Iterations =3D 500000;
	Info.Increment =3D 3;
=09
	Info.Data =3D 0;
	Info.Unlock =3D &NoUnlock;
	Info.Lock =3D &NoLock;
	for (i=3D0; i<threadcount; ++i) {
		pthread_create(&tid[i], NULL, TestThread, &Info);
	}
	for (i=3D0; i<threadcount; ++i)
		pthread_join(tid[i], NULL);
	logit("NoLock Data           =3D %d\n", Info.Data);
	logit("Expected              =3D %d\n", Info.Iterations*Info.Increment*thr=
eadcount);
	Info.Data =3D 0;
	Info.Unlock =3D &PthreadUnlock;
	Info.Lock =3D &PthreadLock;
	for (i=3D0; i<threadcount; ++i) {
		pthread_create(&tid[i], NULL, TestThread, &Info);
	}
	for (i=3D0; i<threadcount; ++i)
		pthread_join(tid[i], NULL);
	logit("pthreads Locking Data =3D %d\n", Info.Data);
	logit("Expected              =3D %d\n", Info.Iterations*Info.Increment*thr=
eadcount);
}


------=_NextPart_000_0098_01C56C0B.2EE7F240
Content-Type: text/plain; charset=us-ascii

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/
------=_NextPart_000_0098_01C56C0B.2EE7F240--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019