freezzo
11-02-2007, 06:58 AM
I'm trying to build a thread manager, which will basically figure out the number of processors available, and split work load up onto separate threads and im running into a problem of getting _beginthread to take the function i want:
#include <process.h>
#include <vector>
using namespace std;
class Threadable
{
public:
Threadable()
{
is_multithreaded = true;
}
virtual void run() = 0;
bool is_multithreaded;
};
class ThreadManager
{
public:
void Add(Threadable *obj)
{
list.push_back(obj);
}
void Execute()
{
std::vector<Threadable *>::const_iterator i;
for(i = list.begin(); i != list.end(); ++i)
{
(*i)->is_multithreaded = true;
(*i)->run();
_beginthread((*i)->run(), 0, 0);
}
}
vector<Threadable *> list;
};
class Test: public Threadable
{
public:
void run()
{
printf("hi\n");
if(is_multithreaded)
_endthread();
}
};
void main(void)
{
ThreadManager *thMan = new ThreadManager();
Test *test = new Test();
Test *test2 = new Test();
thMan->Add(test);
thMan->Add(test);
thMan->Execute();
}
The logic isnt in yet to split up workload, just trying to pass it to beginthread. Any ideas what I am doing wrong?
I added all the code here, its actually in separate class files. Here is my error:
c:\test\input_handler\thread_manager.h(33) : error C2664: '_beginthread' : cannot convert parameter 1 from 'void' to 'void (__cdecl *)(void *)'
I have used _beginthread before with normal functions, I Just cant figure out how to use it with a function referenced from a class stored in the vector.
Thanks
#include <process.h>
#include <vector>
using namespace std;
class Threadable
{
public:
Threadable()
{
is_multithreaded = true;
}
virtual void run() = 0;
bool is_multithreaded;
};
class ThreadManager
{
public:
void Add(Threadable *obj)
{
list.push_back(obj);
}
void Execute()
{
std::vector<Threadable *>::const_iterator i;
for(i = list.begin(); i != list.end(); ++i)
{
(*i)->is_multithreaded = true;
(*i)->run();
_beginthread((*i)->run(), 0, 0);
}
}
vector<Threadable *> list;
};
class Test: public Threadable
{
public:
void run()
{
printf("hi\n");
if(is_multithreaded)
_endthread();
}
};
void main(void)
{
ThreadManager *thMan = new ThreadManager();
Test *test = new Test();
Test *test2 = new Test();
thMan->Add(test);
thMan->Add(test);
thMan->Execute();
}
The logic isnt in yet to split up workload, just trying to pass it to beginthread. Any ideas what I am doing wrong?
I added all the code here, its actually in separate class files. Here is my error:
c:\test\input_handler\thread_manager.h(33) : error C2664: '_beginthread' : cannot convert parameter 1 from 'void' to 'void (__cdecl *)(void *)'
I have used _beginthread before with normal functions, I Just cant figure out how to use it with a function referenced from a class stored in the vector.
Thanks