Link to home
Start Free TrialLog in
Avatar of cossy74
cossy74

asked on

Thread Class - newbie question

Hi,

I am very new to c++ threading. I wish to create a thread class that i can inherit from that can interact with the process that initailly called it. I need to be able to create the thread and send information to it so it can do something and then respond to the process that created it.

Can anyone help?
Avatar of cossy74
cossy74

ASKER

OK here is what i have so far:

//--- HEADER FILE--------------------------------------------------------------------------------------------------------------------

#ifndef __THREAD_H__
#define __THREAD_H__


class Thread
{
public:
    virtual ~Thread() {}
    void run();
    unsigned long pid() const { return m_pid; }
    int IsRunning() const{ return running; }

protected:
    Thread();
    virtual void code() = 0;
    int running;

private:
   
    unsigned long m_pid;

    static void dispatch(void* thread_obj);
};

#endif

//--- CODE FILE -----------------------------------------------------------------------------------------------------------------

#include "stdafx.h"
#include "Thread.h"

#include <windows.h>
#include <process.h>

Thread::Thread()
    : running(0)
    , m_pid(0L)
{
}

void Thread::run()
{
    // Don't start two threads on the same object
    if (running) return;

    // Create an OS thread, using the static callback
    m_pid = _beginthread(Thread::dispatch, 0, this);
    running = 1;
}


void Thread::dispatch(void* thread_obj)
{
    // Call the actual OO thread code
    ((Thread*)thread_obj)->code();

    // After code() returns, kill the thread object
    delete (Thread*)thread_obj;
}

//----------------------------------------------------------------------------------------------------------------------------

This class is threading fine.

I wish to send information between the threads.. what do i need to do?
SOLUTION
Avatar of sushrut
sushrut

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cossy74

ASKER

sushrut: Thanks for the direction.
However i am a C# programmer being forced to maintain code written in c++ of which i know virtually nothing. I only asked here because i needed a quick solution because the powers that be are breathing down my neck.
P.S. i understand your assumption about being a student though :)
Avatar of cossy74

ASKER

OK: Heres what i have:

Parent Class has a public queue<T> variable.
Parent Class fires off a thread A to do some work.
At the end of thread A doing its work it needs to insert into the Parent Class public queue<T> variable.

How can the thread A see and access the Parent Class public queue<T> variable?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial