Re: A question about threads



I had the same requirement. I also needed to write to a status bar from
my other threads. Based on feedback from this mailing list here is what
worked perfectly for me:

Open a pipe in your main GUI code and monitor it.

[code]
/* Create the pipe and then set the watch function */
    if (pipe(status_pipe) != 0) {
        perror("Pipe");
        exit(-1);
    }
    
    fd_flags = fcntl(status_pipe[1], F_GETFL);
    if (fd_flags == -1) {
        perror("read discriptor flags");
        exit(-4);
    }
    
    if (fcntl(status_pipe[1], F_SETFL, fd_flags | O_NONBLOCK) == -1) {
        perror("write descriptor flags");
        exit(-4);
    }
    
    g_status_in = g_io_channel_unix_new(status_pipe[0]);
    g_io_channel_set_encoding(g_status_in, NULL, &err);
    if (err != NULL) {
        fprintf(stderr, "g_io_channel_set_encoding failed: %s\n",
err->message);
        exit(-4);
    }
    
    g_io_channel_set_flags(g_status_in,
g_io_channel_get_flags(g_status_in) | G_IO_FLAG_NONBLOCK, &err);
    if (err != NULL) {
        fprintf(stderr, "g_io_set_flags failed: %s\n", err->message);
        exit(-4);
    }
    
    g_io_add_watch(g_status_in, G_IO_IN | G_IO_PRI, deliver_status, NULL);
    gtk_main();
[/code]

Then in your 'clicked' handler I spawned off a new thread using pthread
because I know my app will only be run on Linux.

[code]
pthread_t tid;
int terr;

terr = pthread_create(&tid, NULL, YOUR_FUNCTION, &YOUR_STRUCTURE);
[/code]

YOUR_STRUCTURE has a parameter for the pipe that was created in main.
This will pass the pipe file descriptor to my other threads which will
write to the pipe. Then 'deliver_status' will read from it and pass the
message on to the status bar handler.

This should hopefully get you in the right direction.


________________________________________________________________________
Try Juno Platinum for Free! Then, only $9.95/month!
Unlimited Internet Access with 1GB of Email Storage.
Visit http://www.juno.com/value to sign up today!





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