DragonFly BSD
DragonFly users List (threaded) for 2005-03
[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]

Question about threading implementation


From: Jonathon McKitrick <jcm@xxxxxxxxxxxxxxxxx>
Date: Sat, 5 Mar 2005 23:00:26 +0000

Hi all,

I've been trying to port my new Linux app from work to run on BSD so I can
work on it at home.  It links with -pthread on Linux, and -lc_r on FreeBSD.
It has a mutex, a lock, and a thread.  The thread runs a loop that sleep()s
a second for 5 seconds, to simulate connecting to the instrument it will
control.  Then it signals a condition.

struct thread_params {
        int             tp_flags;
        pthread_t       tp_tid;
        pthread_mutex_t tp_lock;
        pthread_cond_t  tp_cond;
};

void *thread_init(void *arg)
{
        struct thread_params *tp = (struct thread_params *)arg;
        int i;

        pthread_mutex_lock(&tp->tp_lock);
        printf("Started thread.\n");

        for (i = 1; i < 5; i++)
        {
                printf("Sleeping.\n");
                sleep(1);
        }

        printf("Waking.\n");

        printf("Stopping thread.\n");
	pthread_cond_signal(&tp->tp_cond);
	pthread_mutex_unlock(&tp->tp_lock);

	printf("Stopped thread.\n");
	return 0;
}

void MyClass::initialize()
{
        static struct thread_params tp;
        int err;

        tp.tp_cond = PTHREAD_COND_INITIALIZER;
        tp.tp_lock = PTHREAD_MUTEX_INITIALIZER;

        err = pthread_mutex_lock(&tp.tp_lock);
        if (err)
                printf("Error on lock:  %d\n", err);
        err = pthread_create(&tp.tp_tid, NULL, thread_init, &tp);
	if (err)
	        printf("Error on thread create: %d\n", err);
        else
        {
                printf("Thread created.\n");
		printf("Waiting...\n");

		pthread_cond_wait(&tp.tp_cond, &tp.tp_lock);

		printf("Signaled!\n");
		pthread_mutex_unlock(&tp.tp_lock);

		printf("Thread joining.\n");
		pthread_join(tp.tp_tid, NULL);
		printf("Thread joined.\n");
	}
}

Linking with libc_r under FreeBSD, the thread runs for 5 seconds, then
signals the condition.

Thread created.
Waiting...
Started thread.
Sleeping.
Sleeping.
Sleeping.
Sleeping.
Waking.
Stopping thread.
Stopped thread.
Signaled!
Thread joining.
Thread joined.

--------------------------------------
When I use -lthread_xu, I get this:

Error on lock:  22
Thread created.
Started thread.
Waiting...
Sleeping.
Signaled!
Thread joining.
Thread joined.

----------------------------------------
There is no 5 second delay from the thread.

When I use -lc_r, I get this:

Fatal error 'Failed to initialise garbage collector mutex or condvar' at
line ? in file /usr/src/lib/libc_r/uthread/uthread_init.c (errno = ?)
zsh: abort (core dumped)  app

What am I doing wrong?

Jonathon McKitrick
--
My other computer is your Windows box.



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