98bc5e9fe0cf53d3435d3d43baa284892cd10fa0
[framework/uifw/elementary.git] / src / examples / efl_thread_1.c
1 #include <Elementary.h>
2 #include <pthread.h>
3
4 static Evas_Object *win = NULL;
5 static Evas_Object *rect = NULL;
6
7 static pthread_t thread_id;
8
9 // BEGIN - code running in my custom pthread instance
10 //
11 static void *
12 my_thread_run(void *arg)
13 {
14    double t = 0.0;
15    
16    for (;;)
17      {
18         ecore_thread_main_loop_begin(); // begin critical
19           { // indented for illustration of "critical" block
20              Evas_Coord x, y;
21              
22              x = 200 + (200 * sin(t));
23              y = 200 + (200 * cos(t));
24              evas_object_move(rect, x - 50, y - 50);
25           }
26         ecore_thread_main_loop_end(); // end critical
27         usleep(1000);
28         t += 0.02;
29      }
30    return NULL;
31 }
32 //
33 // END - code running in my custom pthread instance
34
35 static void
36 my_thread_new(void)
37 {
38    pthread_attr_t attr;
39    
40    if (pthread_attr_init(&attr) != 0)
41       perror("pthread_attr_init");
42    if (pthread_create(&thread_id, &attr, my_thread_run, NULL) != 0)
43       perror("pthread_create");
44 }
45
46 int
47 elm_main(int argc, char **argv)
48 {
49    Evas_Object *o, *bg;
50    
51    win = elm_win_add(NULL, "efl-thread-1", ELM_WIN_BASIC);
52    elm_win_title_set(win, "EFL Thread 1");
53    evas_object_resize(win, 400, 400);
54    evas_object_show(win);
55    
56    bg = elm_bg_add(win);
57    elm_win_resize_object_add(win, bg);
58    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
59    evas_object_show(bg);
60    
61    o = evas_object_rectangle_add(evas_object_evas_get(win));
62    evas_object_color_set(o, 50, 80, 180, 255);
63    evas_object_resize(o, 100, 100);
64    evas_object_show(o);
65    rect = o;
66    
67    // create custom thread to do some "work on the side"
68    my_thread_new();
69    
70    elm_run();
71    return 0;
72 }
73
74 ELM_MAIN()