This is the same as \mbox{\hyperlink{efl_thread_4}{EFL Threading example 4}} but now uses the ecore\+\_\+thread infrastructure to have a running worker thread that feeds results back to the mainloop and can easily be cancelled.

This saves some code in the application and makes for fewer problem spots if you forget a mutex.

\mbox{\hyperlink{efl_thread_6}{EFL Threading example 6}}


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{comment}{//Compile\ with:}}
\DoxyCodeLine{\textcolor{comment}{//gcc\ -\/o\ efl\_thread\_5\ efl\_thread\_5.c\ -\/g\ `pkg-\/config\ -\/-\/cflags\ -\/-\/libs\ elementary`}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <Elementary.h>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Ecore__Thread__Group_ga0c2e1f8d562ccf80ecc0009ada72496f}{Ecore\_Thread}}\ *thr\ =\ NULL;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object}}\ *win\ =\ NULL;}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object}}\ *rect\ =\ NULL;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{struct\ }info}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{double}\ x,\ y;}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{//\ BEGIN\ -\/\ code\ running\ in\ my\ custom\ thread\ instance}}
\DoxyCodeLine{\textcolor{comment}{//}}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{th\_do(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \mbox{\hyperlink{group__Ecore__Thread__Group_ga0c2e1f8d562ccf80ecc0009ada72496f}{Ecore\_Thread}}\ *th)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{double}\ t\ =\ 0.0;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{comment}{//\ inside\ our\ "{}do"{}\ function\ for\ the\ ecore\ thread,\ lets\ do\ the\ real\ work}}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{for}\ (;;)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{struct\ }info\ *inf\ =\ malloc(\textcolor{keyword}{sizeof}(\textcolor{keyword}{struct}\ info));}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (inf)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ inf-\/>x\ =\ 200\ +\ (200\ *\ sin(t));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ inf-\/>y\ =\ 200\ +\ (200\ *\ cos(t));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ now\ we\ have\ recorded\ the\ timepoint\ we\ pass\ it\ as\ feedback}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ back\ to\ the\ mainloop.\ it\ will\ free\ it\ when\ done}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_ga8fadd19caae011ab1483665f09ce4d91}{ecore\_thread\_feedback}}(th,\ inf);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{comment}{//\ and\ sleep\ and\ loop}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ usleep(1000);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ t\ +=\ 0.02;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{comment}{//\ in\ case\ someone\ has\ asked\ us\ to\ cancel\ -\/\ then\ cancel\ this\ loop}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{comment}{//\ co-\/operatively\ (cancelling\ is\ co-\/operative)}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (\mbox{\hyperlink{group__Ecore__Thread__Group_ga792471a7ed00718b71d58b791ddbd08d}{ecore\_thread\_check}}(th))\ \textcolor{keywordflow}{break};}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{\}}
\DoxyCodeLine{\textcolor{comment}{//}}
\DoxyCodeLine{\textcolor{comment}{//\ END\ -\/\ code\ running\ in\ my\ custom\ thread\ instance}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}\ \textcolor{comment}{//\ when\ mainloop\ gets\ feedback\ from\ worker}}
\DoxyCodeLine{th\_feedback(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \mbox{\hyperlink{group__Ecore__Thread__Group_ga0c2e1f8d562ccf80ecc0009ada72496f}{Ecore\_Thread}}\ *th\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{void}\ *msg)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{struct\ }info\ *inf\ =\ msg;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group_ga335c8ee6eaeaac9710656af4f8b0db91}{evas\_object\_move}}(rect,\ inf-\/>x\ -\/\ 50,\ inf-\/>y\ -\/\ 50);}
\DoxyCodeLine{\ \ \ free(inf);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{//\ BONUS\ (optional):\ called\ after\ th\_do\ returns\ and\ has\ NOT\ been\ cancelled}}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}\ th\_end(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \mbox{\hyperlink{group__Ecore__Thread__Group_ga0c2e1f8d562ccf80ecc0009ada72496f}{Ecore\_Thread}}\ *th\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})\ \{\ printf(\textcolor{stringliteral}{"{}thread\ ended\(\backslash\)n"{}});\ \}}
\DoxyCodeLine{\textcolor{comment}{//\ BONUS\ (optional):\ called\ in\ mainloop\ AFTER\ thread\ has\ finished\ cancelling}}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}\ th\_cancel(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \mbox{\hyperlink{group__Ecore__Thread__Group_ga0c2e1f8d562ccf80ecc0009ada72496f}{Ecore\_Thread}}\ *th\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})\ \{\ printf(\textcolor{stringliteral}{"{}thread\ cancelled\(\backslash\)n"{}});\ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{//\ just\ test\ cancelling\ the\ thread\ worker}}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{down(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \mbox{\hyperlink{group__Evas__Canvas_ga5ff87cc4ce6bc43e3b640a6d37f73043}{Evas}}\ *e\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \mbox{\hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object}}\ *obj\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{void}\ *event\_info\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (thr)\ \mbox{\hyperlink{group__Ecore__Thread__Group_gae9b9b20e1ed53c62c51617b55aed2511}{ecore\_thread\_cancel}}(thr);}
\DoxyCodeLine{\ \ \ thr\ =\ NULL;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{//\ on\ window\ delete\ -\/\ cancel\ thread\ then\ delete\ window\ and\ exit\ mainloop}}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{del(\textcolor{keywordtype}{void}\ *data\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \mbox{\hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object}}\ *obj,\ \textcolor{keywordtype}{void}\ *event\_info\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (thr)\ \mbox{\hyperlink{group__Ecore__Thread__Group_gae9b9b20e1ed53c62c51617b55aed2511}{ecore\_thread\_cancel}}(thr);}
\DoxyCodeLine{\ \ \ thr\ =\ NULL;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group__Basic_ga9217cf1d90690dbd1960db155d1c7476}{evas\_object\_del}}(obj);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Elm__General_gabada1ccecc6a712df48e90cbf9915c35}{elm\_exit}}();}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{EAPI\_MAIN\ \textcolor{keywordtype}{int}}
\DoxyCodeLine{elm\_main(\textcolor{keywordtype}{int}\ argc\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{char}\ **argv\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object}}\ *o;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Elm__General_ga7a05f37fd3a019da5e26c5fc91fcd937}{elm\_policy\_set}}(\mbox{\hyperlink{group__Elm__General_ggaa16f3378ecdbf660fa378402e3c5a0a9a02fadb6faada04b19d0fa42c11a8d32a}{ELM\_POLICY\_QUIT}},\ \mbox{\hyperlink{group__Elm__General_gga46af37e6ac2c406321060be827117196abbe0962d88ae12afe1a173da881f56cd}{ELM\_POLICY\_QUIT\_LAST\_WINDOW\_CLOSED}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ win\ =\ \mbox{\hyperlink{group__Elm__Win__Group_ga23d9d1ffa0a65a1a1dd482d570db1ba0}{elm\_win\_util\_standard\_add}}(\textcolor{stringliteral}{"{}efl-\/thread-\/5"{}},\ \textcolor{stringliteral}{"{}EFL\ Thread\ 5"{}});}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Smart__Object__Group_gac12dd6655f488cc47ce6cf00eb7d12b2}{evas\_object\_smart\_callback\_add}}(win,\ \textcolor{stringliteral}{"{}delete,request"{}},\ del,\ NULL);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ o\ =\ \mbox{\hyperlink{group__Evas__Object__Rectangle_ga1b7a9d8e6ce240590c1734da5f3449d2}{evas\_object\_rectangle\_add}}(\mbox{\hyperlink{group__Evas__Object__Group_ga7e211219d11abced5f3089362f7476b9}{evas\_object\_evas\_get}}(win));}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group__Basic_gab751e9d24c16e74af6f7c08ea5b930c2}{evas\_object\_color\_set}}(o,\ 50,\ 80,\ 180,\ 255);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group_gacad94595f83580c18908cb3b69b08d1a}{evas\_object\_resize}}(o,\ 100,\ 100);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group__Basic_ga07208fcafd4739de606bc35fff684bf5}{evas\_object\_show}}(o);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group__Events_gac71a1038dad71da36f57b70c8ad10252}{evas\_object\_event\_callback\_add}}(o,\ \mbox{\hyperlink{Evas__Common_8h_ad64cde0da38a60e7cb7905b3ab216968a9fa52651ccf7e47cd3b7785fc4cf393f}{EVAS\_CALLBACK\_MOUSE\_DOWN}},\ down,\ NULL);}
\DoxyCodeLine{\ \ \ rect\ =\ o;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{comment}{//\ explicitly\ create\ ecore\ thread\ to\ do\ some\ "{}work\ on\ the\ side"{}\ and\ pass}}
\DoxyCodeLine{\ \ \ \textcolor{comment}{//\ in\ NULL\ as\ data\ ptr\ to\ callbacks\ and\ true\ at\ the\ end\ means\ to\ actually}}
\DoxyCodeLine{\ \ \ \textcolor{comment}{//\ make\ a\ new\ thread\ and\ not\ use\ the\ thread\ pool\ (there\ is\ a\ thread\ pool}}
\DoxyCodeLine{\ \ \ \textcolor{comment}{//\ with\ as\ many\ thread\ workers\ as\ there\ are\ cpu's\ so\ this\ means\ you\ do\ not}}
\DoxyCodeLine{\ \ \ \textcolor{comment}{//\ overload\ the\ cpu's\ with\ more\ work\ than\ you\ actually\ have\ processing}}
\DoxyCodeLine{\ \ \ \textcolor{comment}{//\ units\ *IF*\ your\ threads\ do\ actually\ spend\ their\ time\ doing\ actual}}
\DoxyCodeLine{\ \ \ \textcolor{comment}{//\ heavy\ computation)}}
\DoxyCodeLine{\ \ \ thr\ =\ \mbox{\hyperlink{group__Ecore__Thread__Group_ga941fa6f0313a6fe551f5d80e2a012bba}{ecore\_thread\_feedback\_run}}(th\_do,\ th\_feedback,\ th\_end,\ th\_cancel,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ NULL,\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group_gacad94595f83580c18908cb3b69b08d1a}{evas\_object\_resize}}(win,\ 400,\ 400);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group__Basic_ga07208fcafd4739de606bc35fff684bf5}{evas\_object\_show}}(win);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Elm__General_gadf2553f83e6b0b9c7fbe054019a664a1}{elm\_run}}();}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (thr)\ \mbox{\hyperlink{group__Ecore__Thread__Group_gae9b9b20e1ed53c62c51617b55aed2511}{ecore\_thread\_cancel}}(thr);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\}}
\DoxyCodeLine{\mbox{\hyperlink{group__Elm__General_ga50fe52d513f7e89c9c770afd76de7ad9}{ELM\_MAIN}}()}

\end{DoxyCodeInclude}
 