multithreaded c++ question

C

Chris Roth

I'm using VS.net 7.1 on Windows XP.

I have a class that hold a container of doubles. One function (foo) for
the class calls a sub-function (bar) for each of the doubles. I'd like
to multithread foo so that the bar sub-functions can run on multiple
threads. I'd like to imlpement this with _beginthreadex as I'm using
std::vector. Please provide some working code around the following details:

#include <windows.h> // for HANDLE
#include <process.h> // for _beginthreadex()
#include <vector>

using namespace std;

class A
{
private:
vector<double> v;
double d; // some other variable common to each thread;
double bar( double x );
public:
vector<double> foo();
};

vector<double> A::foo()
{
vector r( v.size() );
for( int i=0; i<int(v.size()); ++i )
r = bar( v );

return r;
}

double A::bar( double x )
{
double r = x*d; // some function using x and d
// obviosly more complicated in the real code...
return r;
}

Now what I'd like is for multiple instances of bar to run on my two
cores. Can you help me please?
 
R

Ron AF Greve

Hi,


Here is an implementation of my multithreading class (meant to derive from
it and overide run method)



unsigned int __stdcall CPProcess::Spawn(CPProcess *Self)
{
Self->Run();

return 0;
}

bool CPProcess::Start()
{
unsigned int ID = 0;

if( StopEvent = CreateEvent( 0, TRUE, FALSE, 0 ) )
{
if( !( ThreadID = reinterpret_cast<HANDLE>( _beginthreadex( 0, 0,
reinterpret_cast<unsigned int (__stdcall *)(void *)>( Spawn ), this, 0,
&ID) ) ) )
{
CloseHandle( StopEvent );
StopEvent = 0;
}
}

return ThreadID != 0;
}

void CPProcess::Stop()
{
SetEvent( StopEvent );

}

bool CPProcess::Wait(ULONG MilliSeconds)
{
if( WaitForSingleObject( ThreadID, MilliSeconds ) == WAIT_OBJECT_0 )
{
CloseHandle( StopEvent );
CloseHandle( ThreadID );
ThreadID = StopEvent = 0;
}

return ThreadID == 0;

}

bool CPProcess::Stopped()
{
return WaitForSingleObject( StopEvent, 0 ) == WAIT_OBJECT_0;
}

void CPProcess::Run()
{
}



Regards, Ron AF Greve

http://www.InformationSuperHighway.eu
 
G

Gianni Mariani

Chris said:
I'm using VS.net 7.1 on Windows XP.

If your question is specific to win32, you would be better off posting
to a windows specific ng.

comp.programming.threads is more on-topic for generic thread issues.

As for the C++ standard, there is no support for threads, all support is
vendor specific. However, the next revision of the standard will have
thread support.
I have a class that hold a container of doubles. One function (foo) for
the class calls a sub-function (bar) for each of the doubles. I'd like
to multithread foo so that the bar sub-functions can run on multiple
threads. I'd like to imlpement this with _beginthreadex as I'm using
std::vector. Please provide some working code around the following details:

#include <windows.h> // for HANDLE

The last thing you want to include is windows.h, it pollutes the
namespace so much that it's not a good choice for an interface.
#include <process.h> // for _beginthreadex()

Try using a threading library for C++ it eliminates this code and will
work cross platform.
#include <vector>

using namespace std;

class A
{
private:
vector<double> v;
double d; // some other variable common to each thread;
double bar( double x );
public:
vector<double> foo();
};

vector<double> A::foo()
{
vector r( v.size() );
for( int i=0; i<int(v.size()); ++i )
r = bar( v );

return r;
}

double A::bar( double x )
{
double r = x*d; // some function using x and d
// obviosly more complicated in the real code...
return r;
}

Now what I'd like is for multiple instances of bar to run on my two
cores. ...


There will be only one instance of bar. I assume you mean multiple threads.
... Can you help me please?

There are a number of ways to do this. Probably the best way in this
case is to use OMP. This code below would parallelize on an OMP capable
compiler.

#include <omp.h>

....

vector<double> A::foo()
{
vector r( v.size() );
#pragma omp parallel for
for( int i=0; i<int(v.size()); ++i )
r = bar( v );

return r;
}

Threads get very complex very quickly. There is alot hidden under the
covers when you use OMP. If you're doing very complex stuff using
threads where there is alot of interaction, you can run into trouble if
you don't understand what OMP is doing for you.
 
J

James Kanze

I'm using VS.net 7.1 on Windows XP.

I'm more familiar with Posix threads, but I think the basic
principles are similar.
I have a class that hold a container of doubles. One function (foo) for
the class calls a sub-function (bar) for each of the doubles. I'd like
to multithread foo so that the bar sub-functions can run on multiple
threads. I'd like to imlpement this with _beginthreadex as I'm using
std::vector.

What is _beginthreadex, and what is its relationship to
std::vector?
Please provide some working code around the following details:
#include <windows.h> // for HANDLE
#include <process.h> // for _beginthreadex()
#include <vector>
using namespace std;
class A
{
private:
vector<double> v;
double d; // some other variable common to each thread;
double bar( double x );
public:
vector<double> foo();
};
vector<double> A::foo()
{
vector r( v.size() );
for( int i=0; i<int(v.size()); ++i )
r = bar( v );
return r;
}

double A::bar( double x )
{
double r = x*d; // some function using x and d
// obviosly more complicated in the real code...
return r;
}
Now what I'd like is for multiple instances of bar to run on my two
cores. Can you help me please?

The first, and most important question, is: does bar modify d in
your actual code? If so, you'll need a lock around each access
to d, which is likely to make the threaded code much, much
slower. Similar considerations apply if you modify the topology
(size or capacity) of any shared vector.

Secondly, what is the size of the vector, and how often is foo()
called. Creating a thread is expensive; if foo() is called a
lot, you'll want to use a pool of threads, so you don't have to
create new threads each time foo is called, especially if the
vectors aren't that big.

Finally: I'd define a few helper objects (maybe just structs) to
define what each thread should do. Maybe something along the
lines of:

struct ThreadData
{
A* object ;
vector< double >* result ;
int first ;
int last ;
ThreadIdType id ;

ThreadData( A* owner, vector<double>*r )
: object( owner )
, result( r )
{
}
} ;

with an overloaded foo:

void
A::foo( ThreadData const& data )
{
for ( int i = data.first ; i < data.last ; ++ i ) {
(*result)[ i ] = bar( v[ i ] ) ;
}
}

If you're starting the thread in foo, you'll need an `extern
"C"' function (or maybe some special Windows linkage) to kick
things off:

extern "C"
void* // or whatever Windows requires...
threadStarter( void* param )
{
ThreadData* data( static_cast< ThreadData* >( param ) ) ;
data->object->foo( *data ) ;
return NULL ; // or whatever...
}

Given this, foo becomes:

vector< double >
A::foo()
{
vector< double > r( v.size() ) ;
static int const threadCount = numberOfCores ;
size_t perThread = v.size() / threadCount ;
size_t extras = v.size() % threadCount ;
size_t currentIndex = 0 ;
std::vector< ThreadData >
threads( threadCount,
ThreadData( this, &r ) ) ;
for ( size_t i = 0 ; i < threadCount ; ++ i ) {
threads[ i ].first = currentIndex ;
currentIndex += perThread ;
if ( extras != 0 ) {
++ currentIndex ;
-- extras ;
}
threads[ i ].last = currentIndex ;
StartThread( &threads[ i ].id, threadStarter,
&threads[ i ] ) ;
}
for ( size_t i = 0 ; i < threadCount ; ++ i ) {
JoinThread( &threads[ i ].id ) ;
}
}

Obviously, you'll have to use whatever the Windows API requires
for StartThread and JoinThread (CreateThread and
WaitForSingleObject, I think). And you really should add some
error handling as well. (Be very careful about leaving the
function once you've started some threads. Those threads are
using a local variable in the function, and will continue using
even if you leave the function. In case of error, you must
somehow cause all already started threads to stop, and then join
with them.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,351
Latest member
RoxiePulli

Latest Threads

Top