2 //gcc -o efl_thread_5 efl_thread_5.c -g `pkg-config --cflags --libs elementary`
3 #include <Elementary.h>
5 static Ecore_Thread *thr = NULL;
7 static Evas_Object *win = NULL;
8 static Evas_Object *rect = NULL;
15 // BEGIN - code running in my custom thread instance
18 th_do(void *data, Ecore_Thread *th)
22 // inside our "do" function for the ecore thread, lets do the real work
25 struct info *inf = malloc(sizeof(struct info));
29 inf->x = 200 + (200 * sin(t));
30 inf->y = 200 + (200 * cos(t));
31 // now we have recorded the timepoint we pass it as feedback
32 // back to the mainloop. it will free it when done
33 ecore_thread_feedback(th, inf);
38 // in case someone has asked us to cancel - then cancel this loop
39 // co-operatively (cancelling is co-operative)
40 if (ecore_thread_check(th)) break;
44 // END - code running in my custom thread instance
46 static void // when mainloop gets feedback from worker
47 th_feedback(void *data, Ecore_Thread *th, void *msg)
49 struct info *inf = msg;
50 evas_object_move(rect, inf->x - 50, inf->y - 50);
54 // BONUS (optional): called after th_do returns and has NOT been cancelled
55 static void th_end(void *data, Ecore_Thread *th) { printf("thread ended\n"); }
56 // BONUS (optional): called in mainloop AFTER thread has finished cancelling
57 static void th_cancel(void *data, Ecore_Thread *th) { printf("thread cancelled\n"); }
59 // just test cancelling the thread worker
61 down(void *data, Evas *e, Evas_Object *obj, void *event_info)
63 ecore_thread_cancel(thr);
67 elm_main(int argc, char **argv)
71 win = elm_win_add(NULL, "efl-thread-5", ELM_WIN_BASIC);
72 elm_win_title_set(win, "EFL Thread 5");
73 elm_win_autodel_set(win, EINA_TRUE);
74 elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
75 evas_object_resize(win, 400, 400);
76 evas_object_show(win);
79 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
80 elm_win_resize_object_add(win, bg);
83 o = evas_object_rectangle_add(evas_object_evas_get(win));
84 evas_object_color_set(o, 50, 80, 180, 255);
85 evas_object_resize(o, 100, 100);
87 evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, down, NULL);
90 // explicitly create ecore thread to do some "work on the side" and pass
91 // in NULL as data ptr to callbacks and true at the end means to actually
92 // make a new thread and not use the thread pool (there is a thread pool
93 // with as many thread workers as there are cpu's so this means you do not
94 // overload the cpu's with more work than you actually have processing
95 // units *IF* your threads do actually spend their time doing actual
97 thr = ecore_thread_feedback_run(th_do, th_feedback, th_end, th_cancel,