Я пытаюсь использовать реализацию потоков Win32 на основе классов для создания потока источника и потока потребителя. Информация типа int x в потребителе обновляется производителем.
Producer and Consumer both inherit from IRunnable
struct IRunnable {
virtual unsigned long run() = 0;
virtual void stop() = 0;
};
, который создает интерфейс для класса Thread,
class Thread {
public:
Thread(IRunnable *ptr=0) {
_runnable = ptr;
_started = false;
_threadHandle = 0;
}
поток создается в потоке класса с помощью
DWORD threadID=0;
_threadHandle = ::CreateThread(0, 0, ThreadProc, this, 0, &threadID);
А
static unsigned long __stdcall ThreadProc(void* ptr)
{
return ((Thread *)ptr)->run();
}
Как я это использовал
int _tmain(int argc, _TCHAR* argv[]) {
//example of usage
Consumer *obj1=0;
Thread *consumerThread=0;
try {
// create the threadable object first
Consumer *obj1 = new Consumer();
// create and start the thread the thread
Thread *consumerThread = new Thread(obj1);
consumerThread->start();
printf("OkCon.\n");
}
catch (ThreadException &e)
{
printf(e.Message.c_str());
}
Producer *obj=0;
Thread *ProducerThread=0;
try {
// create the threadable object first
Producer *obj = new Producer();
obj->Init(obj1);
// create and start the thread the thread
Thread *ProducerThread = new Thread(obj);
ProducerThread->start();
printf("OkProdu.\n");
}
catch (ThreadException &e)
{
printf(e.Message.c_str());
}
for(int i = 0; i<1000000; i++)
{int a = i;}// just lets the program run on a bit so the threads can run and do a bit more work
delete obj;
delete ProducerThread;
delete obj1;
delete consumerThread;
return 0;
}
Функция запуска для потребителя:
unsigned long Consumer::run()
{
while(_continue)
{
printf("readX, %d \n",x);
}
return 0;
}
Функция инициализации и функция запуска для производителя:
void Producer::Init(Consumer* aConsumer)
{
consData = aConsumer;
}
unsigned long Producer::run()
{
while(_continue)
{
this->consData->x = 1;
}
return 0;
}
Thread :: Run is
unsigned long run() {
_started = true;
unsigned long threadExitCode = _runnable->run();
_started = false;
return threadExitCode;
}
Когда я запускаю код, я получаю необработанное исключение. Место записи нарушения прав доступа 0X ... в строке this-> consData-> x = 1;
Любая помощь будет принята с благодарностью.