2 //gcc -o efl_thread_4 efl_thread_4.c -g `pkg-config --cflags --libs elementary`
3 #include <Elementary.h>
6 static Evas_Object *win = NULL;
7 static Evas_Object *rect = NULL;
14 static void my_thread_mainloop_code(void *data);
16 static pthread_t thread_id;
17 static pthread_mutex_t th_lock;
18 static int th_exit = 0;
20 // BEGIN - code running in my custom pthread instance
23 my_thread_run(void *arg)
27 // inside the pthread function lets loop forever incrementing a time point
30 struct info *inf = malloc(sizeof(struct info));
35 inf->x = 200 + (200 * sin(t));
36 inf->y = 200 + (200 * cos(t));
37 // now call a function in the mainloop and pass it our allocated
38 // data that it will free when it gets it
39 ecore_main_loop_thread_safe_call_async
40 (my_thread_mainloop_code, inf);
45 // in case someone has asked us to cancel - then cancel this loop
46 // co-operatively (cancelling is co-operative)
47 pthread_mutex_lock(&th_lock);
49 pthread_mutex_unlock(&th_lock);
55 // END - code running in my custom pthread instance
62 pthread_mutex_init(&th_lock, NULL);
63 if (pthread_attr_init(&attr) != 0)
64 perror("pthread_attr_init");
65 if (pthread_create(&thread_id, &attr, my_thread_run, NULL) != 0)
66 perror("pthread_create");
70 my_thread_mainloop_code(void *data)
72 struct info *inf = data;
73 evas_object_move(rect, inf->x - 50, inf->y - 50);
77 // just test cancelling the thread
79 down(void *data, Evas *e, Evas_Object *obj, void *event_info)
81 pthread_mutex_lock(&th_lock);
83 pthread_mutex_unlock(&th_lock);
86 // on window delete - cancel thread then delete window and exit mainloop
88 del(void *data, Evas_Object *obj, void *event_info)
90 pthread_mutex_lock(&th_lock);
92 pthread_mutex_unlock(&th_lock);
98 elm_main(int argc, char **argv)
102 elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
104 win = elm_win_util_standard_add("efl-thread-4", "EFL Thread 4");
105 evas_object_smart_callback_add(win, "delete,request", del, NULL);
107 o = evas_object_rectangle_add(evas_object_evas_get(win));
108 evas_object_color_set(o, 50, 80, 180, 255);
109 evas_object_resize(o, 100, 100);
111 // new in the examples - we have a mouse down on the blue box cancel
113 evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, down, NULL);
116 // create custom thread to do some "work on the side"
119 evas_object_resize(win, 400, 400);
120 evas_object_show(win);
123 pthread_mutex_lock(&th_lock);
125 pthread_mutex_unlock(&th_lock);