Reputation: 1
I built a code using multithreading and it works just fine as it is. When I click the button it start the loop writing in the console and when I click again it stops.
But, when I try to implement my real code there an error pops up: error corrupted double linked list
.
I thinks it's because Thread are in the same memory space. I'm thinking about switching to process instead. Is there any library? Because all I can find are fork() and exec() and i'm not sure what I can do with them.
So how do I handle multiprocessing in a gtk application?
#include <pthread.h>
#include <gtk/gtk.h>
int CLICK;
void* fonc(void* arg){
int i;
while (CLICK==1){
printf("nope \n");
usleep(100000); //wait
}
}
static void cb_clicked( GtkButton *button, gpointer data )
{
pthread_t interface, lancementWhile; //déclaration des deux tâches
/* No need to call gdk_threads_enter/gdk_threads_leave,
since gtk callbacks are executed withing main lock. */
if (CLICK==0){
CLICK=1;
pthread_create(&lancementWhile, NULL, fonc, (void*)NULL);
printf("you started \n");
gtk_button_set_label( button, "Clicked" );
}
else{
CLICK=0;
printf("you stoped \n");
gtk_button_set_label( button, "Unclicked" );
}
}
void *creationInterface(void *arg){
GtkWidget *window;
GtkWidget *button;
/* Do stuff as usual */
gtk_init( NULL, NULL );
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
g_signal_connect( G_OBJECT( window ), "destroy",
G_CALLBACK( gtk_main_quit ), NULL );
button = gtk_button_new_with_label( "Initial value" );
g_signal_connect( G_OBJECT( button ), "clicked",
G_CALLBACK( cb_clicked ), NULL );
gtk_container_add( GTK_CONTAINER( window ), button );
gtk_widget_show_all( window );
gtk_main();
}
int main(void)
{
/* Secure glib */
if( ! g_thread_supported() )
g_thread_init( NULL );
/* Secure gtk */
gdk_threads_init();
/* Obtain gtk's global lock */
gdk_threads_enter();
CLICK=0;
pthread_t interface;
pthread_create(&interface, NULL, creationInterface, NULL);
pthread_join(interface, NULL);
}
Upvotes: 0
Views: 200
Reputation: 111
gtk_main() should be call from main only and you should use flag to control threads.
Upvotes: 2