Why cant I access my thread? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Why cant I access my thread?

Post by parad0x13 » Fri Mar 06, 2009 4:06 am

I have a pointer to a thread class, the thread is made in main.cpp and that is where the pointer is created

I want to access the parallel thread in another function from another source file, I can access the pointer just fine with:

Code: Select all

Pointer->ThreadLink->AnyThreadFunction();
but when it tries to run the function I get access violations!

Is there anyone who might know why I cannot access my thread function from a function that did not call it?

- Thank you!

Grrr
Earned some good credits
Earned some good credits
Posts: 126
Joined: Fri Apr 11, 2008 8:48 am
Location: Netherlands

Post by Grrr » Fri Mar 06, 2009 7:35 am

Maybe because the thread is no longer running? Maybe because any of the pointers "Pointer" and "ThreadLink" are no longer valid?

parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Post by parad0x13 » Fri Mar 06, 2009 12:56 pm

I've determined that this is what I'm trying to accomplish, in a nutshell:

I want to be able to start a specific thread "fooThread" from any function "bar_1(), bar_2(), bar_3(), etc..." but each function is in a different .cpp source file, so I need to create a pointer in each bar() function that references to fooThread:

Bar_1.h

Code: Select all

#include "Pointers.h" <-- Centralized pointer class

class bar_1()
{
public:
	Pointers *PL;

	bar();
	~bar(){};
};
Bar_1.cpp

Code: Select all

#include "Bar_1.h"

bar::bar()
{
	// Blah blah blah
	PL->fooThreadLink->SomethingFunction();
}
The pointer class looks like this:
Pointers.h

Code: Select all

//Proper ifndefs etc...
#include "fooThread.h"

class bar_1
class bar_2
class bar_3

class Pointers
{
public:
	Pointers(){};
	~Pointers(){};

	fooThreadLink *fooThread;
};
//endif defs
And I can imagine you can guess what the fooThread code looks like.

The pointers are assigned in my OnInit() function of MainApp, along with the pointer class being created.

My question, is how do I go about creating a fooThread in any bar() function and doing whatever I wish with it?

I know this may seem a little confusing, but if any clarifications are required I'd be happy to do so, for the sake of fixing my problem...

thank you!

computerquip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Fri Feb 20, 2009 7:13 pm
Location: $(#wx)\src

What the hell?

Post by computerquip » Fri Mar 06, 2009 8:51 pm

OKAY OKAY, I think I've got what your trying to say.

So you have a bunch of .cpp files that you want to have access to a specific thread. But when you try to access this, it gives you a Access Violation error. Now, what I need to know specifically is the exact error. Also, give us a hierarchy of the files setup and the declaration of each class in the header so I can figure out why it's giving you an access violation error. Learning experience for all!

Giles
Knows some wx things
Knows some wx things
Posts: 31
Joined: Sat Jan 20, 2007 12:36 pm

Post by Giles » Fri Mar 06, 2009 9:21 pm

It is not clear if your thread is a singleton created by one of the 3 classes or if there can be several threads one per bar instance created.

If you want a singleton, declare the pointer as a static pointer variable in fooThread class

If you want as many threads as bar class instances, you have to declare an instance of the pointer as a member variable in each of the bar classes or declare the thread as a member of the bar classes (if there is no dependency issue between fooThread and bar classes).

In any case get rid of the pointers class. That's really awfull.

Singleton case (single thread is shared between the bar)

fooThread.h

Code: Select all

class fooThreadType : public wxThread
{
public:
    static fooThreadType *fooThreadLink;

    static void CreateSingleton (void)
    {
        if (fooThreadLink == NULL)
            fooThreadLink = new fooThreadType;
    }

    void SomethingFunction(void);
};
fooThread.cpp

Code: Select all

fooThreadType *fooThreadType::fooThreadLink = NULL;
Bar_1.cpp

Code: Select all

#include "fooThread.h"
bar::bar()
{
    CreateSingleton();
    // Blah blah blah
    fooThreadType::fooThreadLink->SomethingFunction();
}

Private thread per bar object


fooThread.h

Code: Select all

class fooThreadType : public wxThread
{
public:
    void SomethingFunction(void);
};
Bar_1.h

Code: Select all

#include "fooThread.h"
class bar
{
private:
    fooThreadType BarThread;
};
Bar_1.cpp

Code: Select all

bar::bar()
{
    BarThread.SomethingFunction();
}
Using Gcc of MinGW 5.1.2 + WxWidget 2.8.7 under XP

parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Post by parad0x13 » Fri Mar 06, 2009 10:18 pm

As I attempt to organize my source and hierarchy to show you how I'm going about my program flow, can someone please tell me how I can get rid of my Pointers class? I really want to, but I don't see how I can!

Giles
Knows some wx things
Knows some wx things
Posts: 31
Joined: Sat Jan 20, 2007 12:36 pm

Post by Giles » Fri Mar 06, 2009 10:37 pm

I provided you with the 2 examples above that show how to proceed without the pointer class depending on what you want to do.

PS: Note that your question is a C++ beginner problem only and has no much thing in common with wxWidget questions which this forum is dedicated to...
Using Gcc of MinGW 5.1.2 + WxWidget 2.8.7 under XP

parad0x13
Experienced Solver
Experienced Solver
Posts: 79
Joined: Tue Jan 06, 2009 3:09 am

Post by parad0x13 » Fri Mar 06, 2009 10:53 pm

Geh, your right... this thread has turned into a programming question, and less wxWidgets

I will research this elsewhere then, thanks for pointing it out

Until I get my code working I shouldn't need to rely on wxWidgets threads

Thank you

Post Reply