guile-user
[Top][All Lists]
Advanced

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

bug: defining things in different threads


From: Linas Vepstas
Subject: bug: defining things in different threads
Date: Tue, 11 Nov 2008 12:57:40 -0600

Hi,

I am finding that things defined in one thread are not always
visible in another.  This seems to be due to some threads
having a different current-module than others.  I think this is a bug.
The example code below shows the issue.  Help, comments
appreciated.

--linas

/**
 * Guile threading bug/unexpected behaviour.
 *
 * Two posix threads are created.  The first thread runs, and defines
 * something. The second thread tries to access the thing defined by
 * the first. But, due to some "unexpected" threading behaviour, the
 * things defined in the first thread are not visible to the first.
 * I'm pretty convinced this is a bug, and am hunting it down now.
 *
 * The printed output of this program, for me, under guile1.8.5, is
 *
 * HEllo, thread one is running
 * Goodbye, thread one is exiting
 * HEllo, thread two is running
 * ERROR: Unbound variable: x
 * Goodbye, thread two is exiting
 * Main is exiting
 *
 * To get a better idea of what's going on, it seems to have something
 * to do with the current module. By turning on print debugging, the
 * output, for me, becomes:
 *
 * HEllo, thread one is running
 * thread one had output:
 * the root module is #<module (guile) f73f9e80>
 * the current module is #<directory (guile-user) f73fc600>
 *
 * Goodbye, thread one is exiting
 * HEllo, thread two is running
 * thread two had output:
 * the root module is #<module (guile) f73f9e80>
 * the current module is #<module (guile) f73f9e80>
 *
 * ERROR: Unbound variable: x
 * Goodbye, thread two is exiting
 * Main is exiting
 */


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

void prtdbg(const char * id)
{
// #define SHOW_STUFF 1
#ifdef SHOW_STUFF
        SCM outport = scm_open_output_string();
        scm_set_current_output_port(outport);

        scm_c_eval_string ("(display \"the root module is \")\n");
        scm_c_eval_string ("(display the-root-module)\n");
        scm_c_eval_string ("(newline)\n");
        scm_c_eval_string ("(display \"the current module is \")\n");
        scm_c_eval_string ("(display (current-module))\n");
        scm_c_eval_string ("(newline)\n");

   SCM out = scm_get_output_string(outport);
   char * str = scm_to_locale_string(out);
   // scm_truncate_file(outport, scm_from_uint16(0));

   printf("%s had output:\n%s\n", id, str);
#endif
}

void * scm_one (void *p)
{
        prtdbg("thread one");
        scm_c_eval_string ("(define x \"asddf\")\n");
}

void * scm_two (void *p)
{
        prtdbg("thread two");
        scm_c_eval_string ("(display x)\n");
}

static void * thread_one (void * arg)
{
        printf("HEllo, thread one is running\n");

        scm_with_guile(scm_one, NULL);

        printf("Goodbye, thread one is exiting\n");
        return NULL;
}

static void * thread_two (void * arg)
{
        printf("HEllo, thread two is running\n");
        scm_with_guile(scm_two, NULL);
        printf("Goodbye, thread two is exiting\n");
        return NULL;
}

main()
{
        int rc;
        pthread_attr_t attr;
        pthread_t t1, t2;

        pthread_attr_init(&attr);

        rc = pthread_create(&t1, &attr, thread_one, NULL);
        if(rc)
        {
                fprintf(stderr, "Fatal error: can't create thread one\n");
                exit(1);
        }
        sleep(1);
        rc = pthread_create(&t2, &attr, thread_two, NULL);
        if(rc)
        {
                fprintf(stderr, "Fatal error: can't create thread two\n");
                exit(1);
        }

        sleep(5);
        printf("Main is exiting\n");
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]