\hypertarget{class_qore_1_1_thread_1_1_counter}{}\section{Qore\+::Thread\+::Counter Class Reference}
\label{class_qore_1_1_thread_1_1_counter}\index{Qore::Thread::Counter@{Qore::Thread::Counter}}


Implements a class that can be used for blocking a thread until a counter reaches zero.  


\subsection*{Public Member Functions}
\begin{DoxyCompactItemize}
\item 
\mbox{\hyperlink{class_qore_1_1_thread_1_1_counter_a99a25b2f06f40edd2535b43ede25ef5a}{constructor}} (softint c=0)
\begin{DoxyCompactList}\small\item\em Creates the \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter}{Counter}} object. \end{DoxyCompactList}\item 
\mbox{\hyperlink{class_qore_1_1_thread_1_1_counter_ac7ee8d36a93135aecdaa6a20ccbd6493}{copy}} ()
\begin{DoxyCompactList}\small\item\em Creates a new \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter}{Counter}} object with the same count as the original. \end{DoxyCompactList}\item 
\mbox{\hyperlink{group__type__conversion__functions_ga2de8717e92c5f97ccc6511f6062d6502}{int}} \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter_a31f5a01212067685e54de06ae29a9984}{dec}} ()
\begin{DoxyCompactList}\small\item\em Atomically decrements the counter value. \end{DoxyCompactList}\item 
\mbox{\hyperlink{class_qore_1_1_thread_1_1_counter_aa300dc50dff017de65e65798e6df14d6}{destructor}} ()
\begin{DoxyCompactList}\small\item\em Destroys the \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter}{Counter}} object. \end{DoxyCompactList}\item 
\mbox{\hyperlink{group__type__conversion__functions_ga2de8717e92c5f97ccc6511f6062d6502}{int}} \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter_a515835de364f97c66142ea565392c8d2}{get\+Count}} ()
\begin{DoxyCompactList}\small\item\em Returns the current counter value. \end{DoxyCompactList}\item 
\mbox{\hyperlink{group__type__conversion__functions_ga2de8717e92c5f97ccc6511f6062d6502}{int}} \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter_a571dea7037a175c74c3d1209b2803738}{get\+Waiting}} ()
\begin{DoxyCompactList}\small\item\em Returns the number of threads currently blocked on this object. \end{DoxyCompactList}\item 
\mbox{\hyperlink{group__type__conversion__functions_ga2de8717e92c5f97ccc6511f6062d6502}{int}} \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter_a7c098cd52caeff8f5ff18dddff2eeeb5}{inc}} ()
\begin{DoxyCompactList}\small\item\em Atomically increments the counter value. \end{DoxyCompactList}\item 
nothing \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter_a1f577aa268217b95e60e4132a1216403}{wait\+For\+Zero}} ()
\begin{DoxyCompactList}\small\item\em Blocks a thread until the counter reaches zero. \end{DoxyCompactList}\item 
\mbox{\hyperlink{group__type__conversion__functions_ga2de8717e92c5f97ccc6511f6062d6502}{int}} \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter_a438ac754cabf7e432bf22d07a34e8c22}{wait\+For\+Zero}} (timeout timeout\+\_\+ms)
\begin{DoxyCompactList}\small\item\em Blocks a thread until the counter reaches zero. \end{DoxyCompactList}\end{DoxyCompactItemize}


\subsection{Detailed Description}
Implements a class that can be used for blocking a thread until a counter reaches zero. 

\begin{DoxyParagraph}{Restrictions\+:}
\mbox{\hyperlink{group__parse__options_gade963e1fbbd1f5b2c777df7221512a1b}{Qore\+::\+P\+O\+\_\+\+N\+O\+\_\+\+T\+H\+R\+E\+A\+D\+\_\+\+C\+L\+A\+S\+S\+ES}}
\end{DoxyParagraph}
\mbox{\hyperlink{class_qore_1_1_thread_1_1_counter}{Counter}} objects allow \mbox{\hyperlink{namespace_qore_1_1_qore}{Qore}} threads to sleep until a counter reaches zero.

\begin{DoxyNote}{Note}
This class is not available with the \mbox{\hyperlink{group__parse__options_gade963e1fbbd1f5b2c777df7221512a1b}{P\+O\+\_\+\+N\+O\+\_\+\+T\+H\+R\+E\+A\+D\+\_\+\+C\+L\+A\+S\+S\+ES}} parse option. 
\end{DoxyNote}


\subsection{Member Function Documentation}
\mbox{\Hypertarget{class_qore_1_1_thread_1_1_counter_a99a25b2f06f40edd2535b43ede25ef5a}\label{class_qore_1_1_thread_1_1_counter_a99a25b2f06f40edd2535b43ede25ef5a}} 
\index{Qore::Thread::Counter@{Qore::Thread::Counter}!constructor@{constructor}}
\index{constructor@{constructor}!Qore::Thread::Counter@{Qore::Thread::Counter}}
\subsubsection{\texorpdfstring{constructor()}{constructor()}}
{\footnotesize\ttfamily Qore\+::\+Thread\+::\+Counter\+::constructor (\begin{DoxyParamCaption}\item[{softint}]{c = {\ttfamily 0} }\end{DoxyParamCaption})}



Creates the \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter}{Counter}} object. 


\begin{DoxyParams}{Parameters}
{\em c} & an argument is supplied here, then the \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter}{Counter}} will be initialized with this value, otherwise the \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter}{Counter}} is initialized with 0\\
\hline
\end{DoxyParams}
\begin{DoxyParagraph}{Example\+:}

\begin{DoxyCode}{0}
\DoxyCodeLine{Counter counter();}
\end{DoxyCode}

\end{DoxyParagraph}

\begin{DoxyExceptions}{Exceptions}
{\em C\+O\+U\+N\+T\+E\+R-\/\+E\+R\+R\+OR} & a negative number was passed to initialize the \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter}{Counter}} \\
\hline
\end{DoxyExceptions}
\mbox{\Hypertarget{class_qore_1_1_thread_1_1_counter_ac7ee8d36a93135aecdaa6a20ccbd6493}\label{class_qore_1_1_thread_1_1_counter_ac7ee8d36a93135aecdaa6a20ccbd6493}} 
\index{Qore::Thread::Counter@{Qore::Thread::Counter}!copy@{copy}}
\index{copy@{copy}!Qore::Thread::Counter@{Qore::Thread::Counter}}
\subsubsection{\texorpdfstring{copy()}{copy()}}
{\footnotesize\ttfamily Qore\+::\+Thread\+::\+Counter\+::copy (\begin{DoxyParamCaption}{ }\end{DoxyParamCaption})}



Creates a new \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter}{Counter}} object with the same count as the original. 

\begin{DoxyParagraph}{Example\+:}

\begin{DoxyCode}{0}
\DoxyCodeLine{Counter new\_counter = counter.copy();}
\end{DoxyCode}
 
\end{DoxyParagraph}
\mbox{\Hypertarget{class_qore_1_1_thread_1_1_counter_a31f5a01212067685e54de06ae29a9984}\label{class_qore_1_1_thread_1_1_counter_a31f5a01212067685e54de06ae29a9984}} 
\index{Qore::Thread::Counter@{Qore::Thread::Counter}!dec@{dec}}
\index{dec@{dec}!Qore::Thread::Counter@{Qore::Thread::Counter}}
\subsubsection{\texorpdfstring{dec()}{dec()}}
{\footnotesize\ttfamily \mbox{\hyperlink{group__type__conversion__functions_ga2de8717e92c5f97ccc6511f6062d6502}{int}} Qore\+::\+Thread\+::\+Counter\+::dec (\begin{DoxyParamCaption}{ }\end{DoxyParamCaption})}



Atomically decrements the counter value. 

A {\ttfamily C\+O\+U\+N\+T\+E\+R-\/\+E\+R\+R\+OR} exception can be thrown if the object is deleted in another thread while this call is in progress; this is a race condition caused by a user programming error and should not occur in practice with correct code.

\begin{DoxyParagraph}{Example\+:}

\begin{DoxyCode}{0}
\DoxyCodeLine{counter.dec();}
\end{DoxyCode}

\end{DoxyParagraph}
\begin{DoxyReturn}{Returns}
the current value after the decrement is returned
\end{DoxyReturn}

\begin{DoxyExceptions}{Exceptions}
{\em C\+O\+U\+N\+T\+E\+R-\/\+E\+R\+R\+OR} & \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter}{Counter}} has been deleted in another thread or \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter}{Counter}} is already at 0\\
\hline
\end{DoxyExceptions}
\begin{DoxySince}{Since}
Qore 0.\+8.\+13 this method returns the current value after the decrement 
\end{DoxySince}
\mbox{\Hypertarget{class_qore_1_1_thread_1_1_counter_aa300dc50dff017de65e65798e6df14d6}\label{class_qore_1_1_thread_1_1_counter_aa300dc50dff017de65e65798e6df14d6}} 
\index{Qore::Thread::Counter@{Qore::Thread::Counter}!destructor@{destructor}}
\index{destructor@{destructor}!Qore::Thread::Counter@{Qore::Thread::Counter}}
\subsubsection{\texorpdfstring{destructor()}{destructor()}}
{\footnotesize\ttfamily Qore\+::\+Thread\+::\+Counter\+::destructor (\begin{DoxyParamCaption}{ }\end{DoxyParamCaption})}



Destroys the \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter}{Counter}} object. 

Note that it is a programming error to delete this object while other threads are blocked on it; in this case an exception is thrown in the deleting thread, and also in each thread blocked on this object when it is deleted.

\begin{DoxyParagraph}{Example\+:}

\begin{DoxyCode}{0}
\DoxyCodeLine{delete counter;}
\end{DoxyCode}

\end{DoxyParagraph}

\begin{DoxyExceptions}{Exceptions}
{\em C\+O\+U\+N\+T\+E\+R-\/\+E\+R\+R\+OR} & Object deleted while other threads blocked on it \\
\hline
\end{DoxyExceptions}
\mbox{\Hypertarget{class_qore_1_1_thread_1_1_counter_a515835de364f97c66142ea565392c8d2}\label{class_qore_1_1_thread_1_1_counter_a515835de364f97c66142ea565392c8d2}} 
\index{Qore::Thread::Counter@{Qore::Thread::Counter}!getCount@{getCount}}
\index{getCount@{getCount}!Qore::Thread::Counter@{Qore::Thread::Counter}}
\subsubsection{\texorpdfstring{getCount()}{getCount()}}
{\footnotesize\ttfamily \mbox{\hyperlink{group__type__conversion__functions_ga2de8717e92c5f97ccc6511f6062d6502}{int}} Qore\+::\+Thread\+::\+Counter\+::get\+Count (\begin{DoxyParamCaption}{ }\end{DoxyParamCaption})}



Returns the current counter value. 

\begin{DoxyReturn}{Returns}
the current counter value
\end{DoxyReturn}
\begin{DoxyParagraph}{Code Flags\+:}
\mbox{\hyperlink{code_flags_CONSTANT}{C\+O\+N\+S\+T\+A\+NT}}
\end{DoxyParagraph}
\begin{DoxyParagraph}{Example\+:}

\begin{DoxyCode}{0}
\DoxyCodeLine{int c = counter.getCount();}
\end{DoxyCode}
 
\end{DoxyParagraph}
\mbox{\Hypertarget{class_qore_1_1_thread_1_1_counter_a571dea7037a175c74c3d1209b2803738}\label{class_qore_1_1_thread_1_1_counter_a571dea7037a175c74c3d1209b2803738}} 
\index{Qore::Thread::Counter@{Qore::Thread::Counter}!getWaiting@{getWaiting}}
\index{getWaiting@{getWaiting}!Qore::Thread::Counter@{Qore::Thread::Counter}}
\subsubsection{\texorpdfstring{getWaiting()}{getWaiting()}}
{\footnotesize\ttfamily \mbox{\hyperlink{group__type__conversion__functions_ga2de8717e92c5f97ccc6511f6062d6502}{int}} Qore\+::\+Thread\+::\+Counter\+::get\+Waiting (\begin{DoxyParamCaption}{ }\end{DoxyParamCaption})}



Returns the number of threads currently blocked on this object. 

\begin{DoxyReturn}{Returns}
the number of threads currently blocked on this object
\end{DoxyReturn}
\begin{DoxyParagraph}{Code Flags\+:}
\mbox{\hyperlink{code_flags_CONSTANT}{C\+O\+N\+S\+T\+A\+NT}}
\end{DoxyParagraph}
\begin{DoxyParagraph}{Example\+:}

\begin{DoxyCode}{0}
\DoxyCodeLine{int c = counter.getWaiting();}
\end{DoxyCode}
 
\end{DoxyParagraph}
\mbox{\Hypertarget{class_qore_1_1_thread_1_1_counter_a7c098cd52caeff8f5ff18dddff2eeeb5}\label{class_qore_1_1_thread_1_1_counter_a7c098cd52caeff8f5ff18dddff2eeeb5}} 
\index{Qore::Thread::Counter@{Qore::Thread::Counter}!inc@{inc}}
\index{inc@{inc}!Qore::Thread::Counter@{Qore::Thread::Counter}}
\subsubsection{\texorpdfstring{inc()}{inc()}}
{\footnotesize\ttfamily \mbox{\hyperlink{group__type__conversion__functions_ga2de8717e92c5f97ccc6511f6062d6502}{int}} Qore\+::\+Thread\+::\+Counter\+::inc (\begin{DoxyParamCaption}{ }\end{DoxyParamCaption})}



Atomically increments the counter value. 

\begin{DoxyParagraph}{Example\+:}

\begin{DoxyCode}{0}
\DoxyCodeLine{counter.inc();}
\end{DoxyCode}

\end{DoxyParagraph}
\begin{DoxyReturn}{Returns}
the current value after the increment is returned
\end{DoxyReturn}
\begin{DoxySince}{Since}
Qore 0.\+9 this method returns the current value after the increment 
\end{DoxySince}
\mbox{\Hypertarget{class_qore_1_1_thread_1_1_counter_a1f577aa268217b95e60e4132a1216403}\label{class_qore_1_1_thread_1_1_counter_a1f577aa268217b95e60e4132a1216403}} 
\index{Qore::Thread::Counter@{Qore::Thread::Counter}!waitForZero@{waitForZero}}
\index{waitForZero@{waitForZero}!Qore::Thread::Counter@{Qore::Thread::Counter}}
\subsubsection{\texorpdfstring{waitForZero()}{waitForZero()}\hspace{0.1cm}{\footnotesize\ttfamily [1/2]}}
{\footnotesize\ttfamily nothing Qore\+::\+Thread\+::\+Counter\+::wait\+For\+Zero (\begin{DoxyParamCaption}{ }\end{DoxyParamCaption})}



Blocks a thread until the counter reaches zero. 

\begin{DoxyParagraph}{Example\+:}

\begin{DoxyCode}{0}
\DoxyCodeLine{counter.waitForZero();}
\end{DoxyCode}

\end{DoxyParagraph}

\begin{DoxyExceptions}{Exceptions}
{\em C\+O\+U\+N\+T\+E\+R-\/\+E\+R\+R\+OR} & \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter}{Counter}} has been deleted in another thread \\
\hline
\end{DoxyExceptions}
\mbox{\Hypertarget{class_qore_1_1_thread_1_1_counter_a438ac754cabf7e432bf22d07a34e8c22}\label{class_qore_1_1_thread_1_1_counter_a438ac754cabf7e432bf22d07a34e8c22}} 
\index{Qore::Thread::Counter@{Qore::Thread::Counter}!waitForZero@{waitForZero}}
\index{waitForZero@{waitForZero}!Qore::Thread::Counter@{Qore::Thread::Counter}}
\subsubsection{\texorpdfstring{waitForZero()}{waitForZero()}\hspace{0.1cm}{\footnotesize\ttfamily [2/2]}}
{\footnotesize\ttfamily \mbox{\hyperlink{group__type__conversion__functions_ga2de8717e92c5f97ccc6511f6062d6502}{int}} Qore\+::\+Thread\+::\+Counter\+::wait\+For\+Zero (\begin{DoxyParamCaption}\item[{timeout}]{timeout\+\_\+ms }\end{DoxyParamCaption})}



Blocks a thread until the counter reaches zero. 


\begin{DoxyParams}{Parameters}
{\em timeout\+\_\+ms} & a \mbox{\hyperlink{data_type_declarations_timeout_type}{timeout}} value to wait for the \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter}{Counter}} to reach zero; integers are interpreted as milliseconds; \mbox{\hyperlink{basic_data_types_relative_dates}{relative date/time values}} are interpreted literally (with a resolution of milliseconds)\\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
0 on sucess, or non-\/zero if a timeout occurred
\end{DoxyReturn}
\begin{DoxyParagraph}{Example\+:}

\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keywordflow}{if} (counter.waitForZero(1500))}
\DoxyCodeLine{    throw \textcolor{stringliteral}{"TIMEOUT"}, \textcolor{stringliteral}{"counter did not reach 0 in 1.5s"};}
\end{DoxyCode}

\end{DoxyParagraph}

\begin{DoxyExceptions}{Exceptions}
{\em C\+O\+U\+N\+T\+E\+R-\/\+E\+R\+R\+OR} & \mbox{\hyperlink{class_qore_1_1_thread_1_1_counter}{Counter}} has been deleted in another thread \\
\hline
\end{DoxyExceptions}
