[sigc] why is this simple test program not thread-safe?



the following simple test program will randomly crash (always in the
same place). the backtrace makes it seem reasonably likely that
sigc::signal::emit() is not thread safe - i.e. an application must
ensure that only a single thread is emitting the same signal at one
time. 

if true, this is a major setback to my understanding of sigc++. i knew
that it was not threadsafe to connect new slots to the signal without
mutexes, but i was under the impression that emission was safe.

--p

compile with: cc -g -o sigctest sigctest.cc `pkg-config --cflags --libs
glibmm-2.4` `pkg-config --cflags --libs gthread`



------------------------

#include <sigc++/signal.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <glibmm/thread.h>

sigc::signal<void,std::string,int> FireMe;
Glib::StaticMutex SyncObject;

void
listener (std::string str, int i)
{
	static int firings;
	Glib::Mutex::Lock lm (SyncObject);
	firings++;
}

void*
signaller (void* arg)
{
	std::string str = (char*) arg;

	while (1) {
		usleep (rand()%100);
		FireMe (str, 1);
	}
	return 0;
}

int
main (int argc, char* argv[])
{
	const char* first_arg = "first";
	const char* second_arg = "second";
	pthread_t first_thread;
	pthread_t second_thread;

	g_thread_init (NULL);

	FireMe.connect (sigc::ptr_fun (listener));

	pthread_create (&first_thread, 0, signaller, (void *) first_arg);
	pthread_create (&second_thread, 0, signaller, (void *) second_arg);

	sleep (-1);
}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]