CYGWIN 1.5.9-1 - Is vprintf() not thread safe?

John William jw2357@hotmail.com
Sun May 23 11:31:00 GMT 2004


<snip>
>How about posting a simple test case.  If you do so, I'll try and take a
>look.
>
>--
>Brian Ford
>Senior Realtime Software Engineer
>VITAL - Visual Simulation Systems
>FlightSafety International
>the best safety device in any aircraft is a well-trained pilot...

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

pthread_mutex_t printf_mutex;

void *thread_exec();
void myprintf(char *,...);

main()
{
	pthread_t id1,id2;
	char *s1="11111";
	char *s2="22222";

	pthread_mutex_init(&printf_mutex,NULL);

	pthread_create(&id1,0,thread_exec,(void *)s1);
	pthread_create(&id2,0,thread_exec,(void *)s2);

	pthread_join(id1,NULL);
	pthread_join(id2,NULL);}

void *thread_exec(string)
	char *string;
{
	int i;

	for (i=0;i<10;i++)
		myprintf("%s (%d)\n",string,i);

	pthread_exit(NULL);
}

void myprintf(char *msg,...)
{
        va_list arg;

/*	pthread_mutex_lock(&printf_mutex);*/

	va_start(arg, msg);
	vprintf(msg, arg);
	va_end(arg);

/*	pthread_mutex_unlock(&printf_mutex);*/
}

When run, I get:

$ gcc t.c ; ./a.exe
11111 (0)
11111 (1)
11111 (2)
11111 (3)
11111 (4)
11111 (5)
11111 (6)11111 (6)

22222 (6)22
22222 (2)
( 22222 (3)
122222 (4))

22222 (5)11111 (8)

22222 (6)11111 (9)

22222 (7)
22222 (8)
22222 (9)

With the mutexes uncommented, I get:

$ gcc t.c ; ./a.exe
22222 (0)
22222 (1)
22222 (2)
22222 (3)
22222 (4)
22222 (5)
11111 (0)
22222 (6)
11111 (1)
22222 (7)
11111 (2)
22222 (8)
11111 (3)
22222 (9)
11111 (4)
11111 (5)
11111 (6)
11111 (7)
11111 (8)
11111 (9)

which is as expected.

For this simple test case, I was not able to get vprintf() to crash, but it 
does on my more complicated application (lots of parameters passed to 
vprintf()). I'm pretty sure it's not a bug in my application as putting in 
the mutex locking around the vprintf() stops the crashes.

Thanks again for your help.

- John

_________________________________________________________________
Stop worrying about overloading your inbox - get MSN Hotmail Extra Storage! 
http://join.msn.click-url.com/go/onm00200362ave/direct/01/


--
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/



More information about the Cygwin mailing list