<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>A Simple Boost Thread Example and Test</title>
<meta name="description" id="description" content="A Simple Boost Thread Example and Test"/>
<meta name="keywords" id="keywords" content=" boost thread example A.1.1c "/>
<style type='text/css'>
body { color : black }
body { background-color : white }
A:link { color : blue }
A:visited { color : purple }
A:active { color : purple }
</style>
<script type='text/javascript' language='JavaScript' src='_a11c_bthread.cpp_xml.js'>
</script>
</head>
<body>
<table><tr>
<td>
<a href="http://www.coin-or.org/CppAD/" target="_top"><img border="0" src="_image.gif"/></a>
</td>
<td><a href="a11c_openmp.cpp.xml" target="_top">Prev</a>
</td><td><a href="a11c_pthread.cpp.xml" target="_top">Next</a>
</td><td>
<select onchange='choose_across0(this)'>
<option>Index-&gt;</option>
<option>contents</option>
<option>reference</option>
<option>index</option>
<option>search</option>
<option>external</option>
</select>
</td>
<td>
<select onchange='choose_up0(this)'>
<option>Up-&gt;</option>
<option>CppAD</option>
<option>multi_thread</option>
<option>thread_test.cpp</option>
<option>a11c_bthread.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>CppAD-&gt;</option>
<option>Install</option>
<option>Introduction</option>
<option>AD</option>
<option>ADFun</option>
<option>preprocessor</option>
<option>multi_thread</option>
<option>library</option>
<option>ipopt_solve</option>
<option>Example</option>
<option>speed</option>
<option>Appendix</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>multi_thread-&gt;</option>
<option>parallel_ad</option>
<option>thread_test.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>thread_test.cpp-&gt;</option>
<option>a11c_openmp.cpp</option>
<option>a11c_bthread.cpp</option>
<option>a11c_pthread.cpp</option>
<option>simple_ad_openmp.cpp</option>
<option>simple_ad_bthread.cpp</option>
<option>simple_ad_pthread.cpp</option>
<option>team_example.cpp</option>
<option>harmonic.cpp</option>
<option>multi_newton.cpp</option>
<option>team_thread.hpp</option>
</select>
</td>
<td>a11c_bthread.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Purpose</option>
<option>Source Code</option>
</select>
</td>
</tr></table><br/>







<center><b><big><big>A Simple Boost Thread Example and Test</big></big></b></center>
<br/>
<b><big><a name="Purpose" id="Purpose">Purpose</a></big></b>
<br/>
This example just demonstrates Boost threads and does not use CppAD at all.

<br/>
<br/>
<b><big><a name="Source Code" id="Source Code">Source Code</a></big></b>

<code><font color="blue">
<br/>
<pre style='display:inline'> 
# include &lt;boost/thread.hpp&gt;
# include &lt;limits&gt;
# include &lt;cmath&gt;
# include &lt;cassert&gt;
# define NUMBER_THREADS 4

namespace { // Begin empty namespace
	class worker_t
	{
	private:
		int    n_;
		float* a_;
		float* b_;
	public:
		void setup(size_t n, float* a, float* b)
		{	n_ = static_cast&lt;int&gt;(n);
			a_ = a;
			b_ = b;
		}
		// Beginning of Example A.1.1.1c of OpenMP 2.5 standard document
		void a1(int n, float *a, float *b)
		{	int i;
			// for some reason this function is missing on some systems
			// assert( bthread_is_multithreaded_np() &gt; 0 );
			for(i = 1; i &lt; n; i++) 
				b[i] = (a[i] + a[i-1]) / 2.0;
			return;
		}
		// End of Example A.1.1.1c of OpenMP 2.5 standard document 
		void operator()()
		{	a1(n_, a_, b_); }
	};
}

bool a11c(void)
{	bool ok = true;

	// Test setup
	size_t i, j, n_total = 10;
	float *a = new float[n_total];
	float *b = new float[n_total];
	for(i = 0; i &lt; n_total; i++)
		a[i] = float(i);

	// number of threads
	size_t number_threads = NUMBER_THREADS;

	// set of workers
	worker_t worker[NUMBER_THREADS];
	// threads for each worker
	boost::thread* bthread[NUMBER_THREADS];

	// Break the work up into sub work for each thread
	size_t  n     = n_total / number_threads;
	size_t  n_tmp = n;
	float*  a_tmp = a;
	float*  b_tmp = b;
	worker[0].setup(n_tmp, a_tmp, b_tmp);
	for(j = 1; j &lt; number_threads; j++)
	{	n_tmp = n + 1;	
		a_tmp = a_tmp + n - 1;
		b_tmp = b_tmp + n - 1;
		if( j == (number_threads - 1) )
			n_tmp = n_total - j * n + 1;

		worker[j].setup(n_tmp, a_tmp, b_tmp);	

		// create this thread
		bthread[j] = new boost::thread(worker[j]);
	}

	// do this threads protion of the work
	worker[0]();

	// wait for other threads to finish
	for(j = 1; j &lt; number_threads; j++)
	{	bthread[j]-&gt;join();	
		delete bthread[j];
	}

	// check the result
	float eps = 100. * std::numeric_limits&lt;float&gt;::epsilon();
	for(i = 1; i &lt; n ; i++)
		ok &amp;= std::fabs( (2. * b[i] - a[i] - a[i-1]) / b[i] ) &lt;= eps; 

	delete [] a;
	delete [] b;

	return ok;
}
</pre>

</font></code>

<hr/>Input File: multi_thread/bthread/a11c_bthread.cpp

</body>
</html>
