Working with threads is hard.

Ecore helps to do so a bit easier, but as the example in \mbox{\hyperlink{ecore_thread_example_8c-example}{ecore\+\_\+thread\+\_\+example.c}} shows, there\textquotesingle{}s a lot to consider even when doing the most simple things.

We\textquotesingle{}ll be going through this thorough example now, showing how the differents aspects of \mbox{\hyperlink{group__Ecore__Thread__Group_ga0c2e1f8d562ccf80ecc0009ada72496f}{Ecore\+\_\+\+Thread}} are used, but users are encourage to avoid threads unless it\textquotesingle{}s really the only option, as they always add more complexity than the program usually requires.

Ecore Threads come in two flavors, short jobs and feedback jobs. Short jobs just run the given function and are more commonly used for small tasks where the main loop does not need to know how the work is going in between. The short job in our example is so short we had to artificially enlarge it with {\ttfamily sleep()}. Other than that, it also uses threads local data to keep the data we are working with persistent across different jobs ran by the same system thread. This data will be freed when the no more jobs are pending and the thread is terminated. If the data doesn\textquotesingle{}t exist in the thread\textquotesingle{}s storage, we create it and save it there for future jobs to find it. If creation fails, we cancel ourselves, so the main loop knows that we didn\textquotesingle{}t just exit normally, meaning the job could not be done. The main part of the function checks in each iteration if it was canceled by the main loop, and if it was, it stops processing and clears the data from the storage (we assume {\ttfamily cancel} means no one else will need this, but this is really application dependent).  
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{\_local\_data\_free(\textcolor{keywordtype}{void}\ *data)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ Thread\_Data\ *td\ =\ data;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ *str;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__List__Group_gae27a2c686a0a11693f9b06957c9bba83}{EINA\_LIST\_FREE}}(td-\/>list,\ str)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}Freeing\ string:\ \%s\(\backslash\)n"{}},\ str);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ free(str);}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ free(td);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{\_short\_job(\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{\ \ \ Thread\_Data\ *td;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ td\ =\ \mbox{\hyperlink{group__Ecore__Thread__Group_gaa99c30f01228217ddd0e510e4e99610b}{ecore\_thread\_local\_data\_find}}(th,\ \textcolor{stringliteral}{"{}data"{}});}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!td)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ td\ =\ calloc(1,\ \textcolor{keyword}{sizeof}(Thread\_Data));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (!td)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_gae9b9b20e1ed53c62c51617b55aed2511}{ecore\_thread\_cancel}}(th);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_ga6878f550a9bcc8002c0c709b4b4dca66}{ecore\_thread\_local\_data\_add}}(th,\ \textcolor{stringliteral}{"{}data"{}},\ td,\ \_local\_data\_free,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}});}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ 10;\ i++)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ buf[200];}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (\mbox{\hyperlink{group__Ecore__Thread__Group_ga792471a7ed00718b71d58b791ddbd08d}{ecore\_thread\_check}}(th))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_gad3c1d51a24772b885b26486be86c2779}{ecore\_thread\_local\_data\_del}}(th,\ \textcolor{stringliteral}{"{}data"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ snprintf(buf,\ \textcolor{keyword}{sizeof}(buf),\ \textcolor{stringliteral}{"{}Thread\ \%p:\ String\ number\ \%d"{}},\ th,\ i);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ td-\/>list\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga625698a383dda8fc0db245a1c7608c49}{eina\_list\_append}}(td-\/>list,\ strdup(buf));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ sleep(1);}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 Feedback jobs, on the other hand, run tasks that will inform back to the main loop its progress, send partial data as is processed, just ping saying it\textquotesingle{}s still alive and processing, or anything that needs the thread to talk back to the main loop. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{\_feedback\_job(\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{\ \ \ time\_t\ t;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{int}\ i,\ count;}
\DoxyCodeLine{\ \ \ Feedback\_Thread\_Data\ *ftd\ =\ NULL;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Eina__Iterator}{Eina\_Iterator}}\ *it;}
\DoxyCodeLine{\ \ \ App\_Msg\ *msg;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ *name;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ count\ =\ (int)(uintptr\_t)\mbox{\hyperlink{group__Ecore__Thread__Group_ga0eea48ec2cec77087c3f246347f26909}{ecore\_thread\_global\_data\_find}}(\textcolor{stringliteral}{"{}count"{}});}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ count;\ i++)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ buf[32];}
\DoxyCodeLine{\ \ \ \ \ \ \ \ snprintf(buf,\ \textcolor{keyword}{sizeof}(buf),\ \textcolor{stringliteral}{"{}data\%d"{}},\ i);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ftd\ =\ \mbox{\hyperlink{group__Ecore__Thread__Group_ga0eea48ec2cec77087c3f246347f26909}{ecore\_thread\_global\_data\_find}}(buf);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (!ftd)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{continue};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (eina\_lock\_take\_try(\&ftd-\/>mutex))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ ftd\ =\ NULL;}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!ftd)}
\DoxyCodeLine{\ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ it\ =\ \mbox{\hyperlink{group__Eina__File__Group_gab744fcd8eb2369fed66520ff82930650}{eina\_file\_ls}}(ftd-\/>base);}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!it)}
\DoxyCodeLine{\ \ \ \ \ \textcolor{keywordflow}{goto}\ the\_end;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ msg\ =\ calloc(1,\ \textcolor{keyword}{sizeof}(App\_Msg));}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ t\ =\ time(NULL);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Iterator__Group_gac7314580e18868ef229f5d2814562794}{EINA\_ITERATOR\_FOREACH}}(it,\ name)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (time(NULL)\ >=\ (t\ +\ 2))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Stringshare__Group_ga5538b4a134987b8a01744fad1a845ba4}{eina\_stringshare\_del}}(name);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \}}

\end{DoxyCodeInclude}
 Finally, one more feedback job, but this one will be running outside of Ecore\textquotesingle{}s pool, so we can use the pool for real work and keep this very light function unchecked. All it does is check if some condition is met and send a message to the main loop telling it it\textquotesingle{}s time to close. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{\_out\_of\_pool\_job(\textcolor{keywordtype}{void}\ *data,\ \mbox{\hyperlink{group__Ecore__Thread__Group_ga0c2e1f8d562ccf80ecc0009ada72496f}{Ecore\_Thread}}\ *th)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ App\_Data\ *ad\ =\ data;}
\DoxyCodeLine{\ \ \ App\_Msg\ *msg;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{while}\ (1)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ msgs;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ eina\_condition\_wait(\&ad-\/>condition);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ msgs\ =\ ad-\/>msgs\_received;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ eina\_lock\_release(\&ad-\/>mutex);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (msgs\ ==\ ad-\/>max\_msgs)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ msg\ =\ calloc(1,\ \textcolor{keyword}{sizeof}(App\_Msg));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ msg-\/>all\_done\ =\ 1;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_ga8fadd19caae011ab1483665f09ce4d91}{ecore\_thread\_feedback}}(th,\ msg);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 Every now and then the program prints its status, counting threads running and pending jobs. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{\_print\_status(\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{int}\ active,\ pending\_total,\ pending\_feedback,\ pending\_short,\ available;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ active\ =\ \mbox{\hyperlink{group__Ecore__Thread__Group_gad7a28588dc26d6af8b6db8abc3ef37ce}{ecore\_thread\_active\_get}}();}
\DoxyCodeLine{\ \ \ pending\_total\ =\ \mbox{\hyperlink{group__Ecore__Thread__Group_gaeaa014287d578a200d41cca861bceac3}{ecore\_thread\_pending\_total\_get}}();}
\DoxyCodeLine{\ \ \ pending\_feedback\ =\ \mbox{\hyperlink{group__Ecore__Thread__Group_gaa5c56445198f7b03c1f080039499689a}{ecore\_thread\_pending\_feedback\_get}}();}
\DoxyCodeLine{\ \ \ pending\_short\ =\ \mbox{\hyperlink{group__Ecore__Thread__Group_ga3e32f650cfe811e69bba7825c71b9070}{ecore\_thread\_pending\_get}}();}
\DoxyCodeLine{\ \ \ available\ =\ \mbox{\hyperlink{group__Ecore__Thread__Group_ga6e7703b3013d9c872449f6e2ec093b53}{ecore\_thread\_available\_get}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Status:\(\backslash\)n\(\backslash\)t*\ Active\ threads:\ \%d\(\backslash\)n"{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}\(\backslash\)t*\ Available\ threads:\ \%d\(\backslash\)n"{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}\(\backslash\)t*\ Pending\ short\ jobs:\ \%d\(\backslash\)n"{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}\(\backslash\)t*\ Pending\ feedback\ jobs:\ \%d\(\backslash\)n"{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \textcolor{stringliteral}{"{}\(\backslash\)t*\ Pending\ total:\ \%d\(\backslash\)n"{}},\ active,\ available,\ pending\_short,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ pending\_feedback,\ pending\_total);}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 In our main loop, we\textquotesingle{}ll be receiving messages from our feedback jobs using the same callback for both of them. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{\_feedback\_job\_msg\_cb(\textcolor{keywordtype}{void}\ *data,\ \mbox{\hyperlink{group__Ecore__Thread__Group_ga0c2e1f8d562ccf80ecc0009ada72496f}{Ecore\_Thread}}\ *th,\ \textcolor{keywordtype}{void}\ *msg\_data)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ App\_Data\ *ad\ =\ data;}
\DoxyCodeLine{\ \ \ App\_Msg\ *msg\ =\ msg\_data;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ *str;}

\end{DoxyCodeInclude}
 The light job running out of the pool will let us know when we can exit our program. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (msg-\/>all\_done)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();}
\DoxyCodeLine{\ \ \ \ \ \ \ \ free(msg);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
 Next comes the handling of data sent from the actual worker threads, always remembering that the data belongs to us now, and not the thread, so it\textquotesingle{}s our responsibility to free it. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \_print\_status();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!msg-\/>list)}
\DoxyCodeLine{\ \ \ \ \ printf(\textcolor{stringliteral}{"{}Received\ an\ empty\ list\ from\ thread\ \%p\(\backslash\)n"{}},\ th);}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ i\ =\ 0;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}Received\ \%d\ elements\ from\ threads\ \%p\ (printing\ first\ 5):\(\backslash\)n"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ eina\_list\_count(msg-\/>list),\ th);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__List__Group_gae27a2c686a0a11693f9b06957c9bba83}{EINA\_LIST\_FREE}}(msg-\/>list,\ str)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (i\ <=\ 5)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ printf(\textcolor{stringliteral}{"{}\(\backslash\)t\%s\(\backslash\)n"{}},\ str);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ free(str);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ i++;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
 Last, the condition to exit is given by how many messages we want to handle, so we need to count them and inform the condition checking thread that the value changed. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ eina\_lock\_take(\&ad-\/>mutex);}
\DoxyCodeLine{\ \ \ ad-\/>msgs\_received++;}
\DoxyCodeLine{\ \ \ eina\_condition\_signal(\&ad-\/>condition);}
\DoxyCodeLine{\ \ \ eina\_lock\_release(\&ad-\/>mutex);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ free(msg);}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 When a thread finishes its job or gets canceled, the main loop is notified through the callbacks set when creating the task. In this case, we just print what happen and keep track of one of them used to exemplify canceling. Here we are pretending one of our short jobs has a timeout, so if it doesn\textquotesingle{}t finish before a timer is triggered, it will be canceled. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{\_thread\_end\_cb(\textcolor{keywordtype}{void}\ *data,\ \mbox{\hyperlink{group__Ecore__Thread__Group_ga0c2e1f8d562ccf80ecc0009ada72496f}{Ecore\_Thread}}\ *th)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ App\_Data\ *ad\ =\ data;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Normal\ termination\ for\ thread\ \%p.\(\backslash\)n"{}},\ th);}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (th\ ==\ ad-\/>thread\_3)}
\DoxyCodeLine{\ \ \ \ \ ad-\/>thread\_3\ =\ NULL;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{\_thread\_cancel\_cb(\textcolor{keywordtype}{void}\ *data,\ \mbox{\hyperlink{group__Ecore__Thread__Group_ga0c2e1f8d562ccf80ecc0009ada72496f}{Ecore\_Thread}}\ *th)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ App\_Data\ *ad\ =\ data;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Thread\ \%p\ got\ cancelled.\(\backslash\)n"{}},\ th);}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (th\ ==\ ad-\/>thread\_3)}
\DoxyCodeLine{\ \ \ \ \ ad-\/>thread\_3\ =\ NULL;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_cancel\_timer\_cb(\textcolor{keywordtype}{void}\ *data)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ App\_Data\ *ad\ =\ data;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (ad-\/>thread\_3\ \&\&\ !\mbox{\hyperlink{group__Ecore__Thread__Group_ga792471a7ed00718b71d58b791ddbd08d}{ecore\_thread\_check}}(ad-\/>thread\_3))}
\DoxyCodeLine{\ \ \ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_gae9b9b20e1ed53c62c51617b55aed2511}{ecore\_thread\_cancel}}(ad-\/>thread\_3);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 The main function does some setup that includes reading parameters from the command line to change its behaviour and test different results. These are\+: \begin{DoxyItemize}
\item -\/t \texorpdfstring{$<$}{<}some\+\_\+num\texorpdfstring{$>$}{>} maximum number of threads to run at the same time. \item -\/p \texorpdfstring{$<$}{<}some\+\_\+path\texorpdfstring{$>$}{>} adds {\ttfamily some\+\_\+path} to the list used by the feedback jobs. This parameter can be used multiple times. \item -\/m \texorpdfstring{$<$}{<}some\+\_\+num\texorpdfstring{$>$}{>} the number of messages to process before the program is signalled to exit.\end{DoxyItemize}
Skipping some bits, we init Ecore and our application data. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ i\ =\ \mbox{\hyperlink{group__Ecore__Thread__Group_gabad7972818b7fb1346ae7a098cd47220}{ecore\_thread\_max\_get}}();}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}Initial\ max\ threads:\ \%d\(\backslash\)n"{}},\ i);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ memset(\&appdata,\ 0,\ \textcolor{keyword}{sizeof}(App\_Data));}
\DoxyCodeLine{\ \ \ appdata.max\_msgs\ =\ 1;}

\end{DoxyCodeInclude}
 If any paths for the feedback jobs were given, we use them, otherwise we fallback to some defaults. Always initializing the proper mutexes used by the threaded job. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!path\_list)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ Feedback\_Thread\_Data\ *ftd;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_ga4e9de7492f8400e19ddd157d8cc21811}{ecore\_thread\_global\_data\_add}}(\textcolor{stringliteral}{"{}count"{}},\ (\textcolor{keywordtype}{void}\ *)3,\ NULL,\ \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ftd\ =\ calloc(1,\ \textcolor{keyword}{sizeof}(Feedback\_Thread\_Data));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ftd-\/>name\ =\ strdup(\textcolor{stringliteral}{"{}data0"{}});}
\DoxyCodeLine{\textcolor{preprocessor}{\#ifdef\ \_WIN32}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ftd-\/>base\ =\ strdup(\textcolor{stringliteral}{"{}c:/windows/System32"{}});}
\DoxyCodeLine{\textcolor{preprocessor}{\#else}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ftd-\/>base\ =\ strdup(\textcolor{stringliteral}{"{}/usr/bin"{}});}
\DoxyCodeLine{\textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ eina\_lock\_new(\&ftd-\/>mutex);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_ga4e9de7492f8400e19ddd157d8cc21811}{ecore\_thread\_global\_data\_add}}(ftd-\/>name,\ ftd,\ NULL,\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ftd\ =\ calloc(1,\ \textcolor{keyword}{sizeof}(Feedback\_Thread\_Data));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ftd-\/>name\ =\ strdup(\textcolor{stringliteral}{"{}data1"{}});}
\DoxyCodeLine{\textcolor{preprocessor}{\#ifdef\ \_WIN32}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ftd-\/>base\ =\ strdup(\textcolor{stringliteral}{"{}c:/windows/Fonts"{}});}
\DoxyCodeLine{\textcolor{preprocessor}{\#else}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ftd-\/>base\ =\ strdup(\textcolor{stringliteral}{"{}/usr/lib"{}});}
\DoxyCodeLine{\textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ eina\_lock\_new(\&ftd-\/>mutex);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_ga4e9de7492f8400e19ddd157d8cc21811}{ecore\_thread\_global\_data\_add}}(ftd-\/>name,\ ftd,\ NULL,\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ftd\ =\ calloc(1,\ \textcolor{keyword}{sizeof}(Feedback\_Thread\_Data));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ftd-\/>name\ =\ strdup(\textcolor{stringliteral}{"{}data2"{}});}
\DoxyCodeLine{\textcolor{preprocessor}{\#ifdef\ \_WIN32}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ftd-\/>base\ =\ strdup(\textcolor{stringliteral}{"{}c:/windows/Help"{}});}
\DoxyCodeLine{\textcolor{preprocessor}{\#else}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ftd-\/>base\ =\ strdup(\textcolor{stringliteral}{"{}/usr/lib"{}});}
\DoxyCodeLine{\textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ eina\_lock\_new(\&ftd-\/>mutex);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_ga4e9de7492f8400e19ddd157d8cc21811}{ecore\_thread\_global\_data\_add}}(ftd-\/>name,\ ftd,\ NULL,\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}});}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ Feedback\_Thread\_Data\ *ftd;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ *str;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_ga4e9de7492f8400e19ddd157d8cc21811}{ecore\_thread\_global\_data\_add}}(\textcolor{stringliteral}{"{}count"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (\textcolor{keywordtype}{void}\ *)(uintptr\_t)eina\_list\_count(path\_list),\ NULL,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ i\ =\ 0;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__List__Group_gae27a2c686a0a11693f9b06957c9bba83}{EINA\_LIST\_FREE}}(path\_list,\ str)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{char}\ buf[32];}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ snprintf(buf,\ \textcolor{keyword}{sizeof}(buf),\ \textcolor{stringliteral}{"{}data\%d"{}},\ i);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ ftd\ =\ calloc(1,\ \textcolor{keyword}{sizeof}(Feedback\_Thread\_Data));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ ftd-\/>name\ =\ strdup(buf);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ ftd-\/>base\ =\ strdup(str);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ eina\_lock\_new(\&ftd-\/>mutex);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_ga4e9de7492f8400e19ddd157d8cc21811}{ecore\_thread\_global\_data\_add}}(ftd-\/>name,\ ftd,\ NULL,\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ free(str);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ i++;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
 Initialize the mutex needed for the condition checking thread 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ eina\_lock\_new(\&appdata.mutex);}
\DoxyCodeLine{\ \ \ eina\_condition\_new(\&appdata.condition,\ \&appdata.mutex);}

\end{DoxyCodeInclude}
 And start our tasks. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_ga941fa6f0313a6fe551f5d80e2a012bba}{ecore\_thread\_feedback\_run}}(\_out\_of\_pool\_job,\ \_feedback\_job\_msg\_cb,\ NULL,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ NULL,\ \&appdata,\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}});}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_ga6857d9e5bd717ae3d8417b39ec585c3f}{ecore\_thread\_run}}(\_short\_job,\ \_thread\_end\_cb,\ \_thread\_cancel\_cb,\ \&appdata);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_ga941fa6f0313a6fe551f5d80e2a012bba}{ecore\_thread\_feedback\_run}}(\_feedback\_job,\ \_feedback\_job\_msg\_cb,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \_thread\_end\_cb,\ \_thread\_cancel\_cb,\ \&appdata,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}});}
\DoxyCodeLine{\ \ \ appdata.thread\_3\ =\ \mbox{\hyperlink{group__Ecore__Thread__Group_ga6857d9e5bd717ae3d8417b39ec585c3f}{ecore\_thread\_run}}(\_short\_job,\ \_thread\_end\_cb,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \_thread\_cancel\_cb,\ \&appdata);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Thread__Group_ga941fa6f0313a6fe551f5d80e2a012bba}{ecore\_thread\_feedback\_run}}(\_feedback\_job,\ \_feedback\_job\_msg\_cb,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \_thread\_end\_cb,\ \_thread\_cancel\_cb,\ \&appdata,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}});}

\end{DoxyCodeInclude}
 To finalize, set a timer to cancel one of the tasks if it doesn\textquotesingle{}t end before the timeout, one more timer for status report and get into the main loop. Once we are out, destroy our mutexes and finish the program. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Timer__Group_ga926423e464d7ef71105dd822d90e1187}{ecore\_timer\_add}}(1.0,\ \_cancel\_timer\_cb,\ \&appdata);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Timer__Group_ga926423e464d7ef71105dd822d90e1187}{ecore\_timer\_add}}(2.0,\ \_status\_timer\_cb,\ NULL);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \_print\_status();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ eina\_condition\_free(\&appdata.condition);}
\DoxyCodeLine{\ \ \ eina\_lock\_free(\&appdata.mutex);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Init__Group_ga768298b932f18d7e7593a447493e5cde}{ecore\_shutdown}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
