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?
cossy74Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cossy74Author Commented:
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?
sushrutCommented:
Good. So you yourself are trying to code. (and not asking us to do your homework) I am sure, you will soon become a star developer.

From Unix perspective you can use seamphonre, mutex, shared memory, files, piptes for IPC.
you can search individual keywords on google and you will get loads of information.

For windows code...
google for "windows IPC" You get loads of links.
http://www.codeproject.com/threads/Win32IPC.asp
http://www.devhood.com/tools/tool_details.aspx?tool_id=803 (and download the document IPC.doc )
You can find many other good links.
cossy74Author Commented:
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 :)
cossy74Author Commented:
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?
krispinCommented:
Why not have a pointer in Thread class to the Parent class and a method to the set the parent class reference?
e.g.

class Thread
{
public:
    virtual ~Thread() {}
    void run();
    unsigned long pid() const { return m_pid; }
    int IsRunning() const{ return running; }
   void setParent(ParentClass *ref){pref = ref;}
   ParentClass *pref;


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

private:    
    unsigned long m_pid;
        static void dispatch(void* thread_obj);
};

---------------------------------------------------------------------------------------

then, in your dispatch implementation
---------------------------------------------------------------------------------------

void Thread::dispatch(void* thread_obj)
{
    // Call the actual OO thread code
    ((Thread*)thread_obj)->code();
   
         ((Thread*)thread_obj)->pref->((some method used to signal the parent class or insert variable or whatever))

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



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.