简体   繁体   English

Win32 线程生产者更新消费者线程

[英]Win32 threads producer updating a consumer thread

I am trying to use a class based implementation of Win32 threads to create a Producer thread and a Consumer thread.我正在尝试使用基于 class 的 Win32 线程实现来创建生产者线程和消费者线程。 Information of type int x in the consumer is updated by the producer.消费者中的 int x 类型的信息由生产者更新。

Producer and Consumer both inherit from IRunnable
struct IRunnable {
    virtual unsigned long run() = 0;
    virtual void stop() = 0;
};

Which creates an interface for class Thread,它为 class 线程创建了一个接口,

class Thread {
public:
    Thread(IRunnable *ptr=0) {
        _runnable = ptr;
        _started = false;
        _threadHandle = 0;
    }

a thread is created in class thread by在 class 线程中创建了一个线程

DWORD threadID=0;
_threadHandle = ::CreateThread(0, 0, ThreadProc, this, 0, &threadID);

And

static unsigned long __stdcall ThreadProc(void* ptr) 
{
return ((Thread *)ptr)->run();
}

How I have used it is我是如何使用它的

    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;
}

The run function for consumer is消费者的运行 function 是

unsigned long Consumer::run()
{
    while(_continue)
    {
        printf("readX, %d \n",x);

    }

    return 0;
}

The init function and run function for producer are生产者的初始化 function 和运行 function 是

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;
    }

When I run the code I get an Unhandled exception.当我运行代码时,我得到一个未处理的异常。 Access violation writing location 0X... at line this->consData->x = 1;访问冲突写入位置 0X... 在 this->consData->x = 1 行;

Any help would be greatly appreciated.任何帮助将不胜感激。

In first try block you are assigning Consumer instance to newly created local variable Consumer *obj1 instead of using existing variable that was created just before try block.在第一个 try 块中,您将 Consumer 实例分配给新创建的局部变量 Consumer *obj1,而不是使用在 try 块之前创建的现有变量。 Try something like this instead:尝试这样的事情:

Consumer *obj1=0;
Thread *consumerThread=0;

try {
    // create the threadable object first
    obj1 = new Consumer();

This modify existing variable instead of creating new one.这会修改现有变量而不是创建新变量。 Same story with Producer *obj, Thread *consumerThread and Thread *ProducerThread. Producer *obj、Thread *consumerThread 和 Thread *ProducerThread 的情况相同。 Please read something about scope and lifetime of variables in C++.请阅读有关 scope 和 C++ 中变量的生命周期的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM