wxThread using pointer to change array data in main thread

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
softport
Experienced Solver
Experienced Solver
Posts: 61
Joined: Sat Jan 28, 2012 3:55 pm
Location: Houston TX
Contact:

wxThread using pointer to change array data in main thread

Post by softport » Sun Jan 13, 2013 11:30 am

Hello, I have been trying to figure out how to pass data from a wx detached thread, to an array in the main thread. After some searching and trial and error, I ended up passing an array pointer to the thread, when the thread is created.

It's working fine, but I'm wondering is it a safe thing to do? This is just a test, modifying one byte at a time in a 64 byte array. In the final version it will be changing all 64 bytes at once.

myThread.h

Code: Select all

#include "wx/wx.h"
 
// the ID we'll use to identify our event
const int THREAD_STARTED_ID  = 100000;
const int THREAD_DATA_UPDATE_ID = 100001;
const int THREAD_STOPPED_ID  = 100002;

// a thread class that will periodically send events to the GUI thread
class MyThread : public wxThread
{
    wxFrame* m_parent;
    unsigned char * ucExternDataArray;
public:

    // ucDataArray Is the address of the array in the main thread
    MyThread(wxFrame* parent, unsigned char * ucDataArray)
    {
        m_parent = parent;
        ucExternDataArray = ucDataArray;
    }
    
    virtual ExitCode Entry();
}; 
myThread.cpp

Code: Select all

#include "myThread.h"

wxThread::ExitCode MyThread::Entry()
{
    wxCommandEvent eventStart( wxEVT_COMMAND_TEXT_UPDATED, THREAD_STARTED_ID );
    wxCommandEvent eventNewData( wxEVT_COMMAND_TEXT_UPDATED, THREAD_DATA_UPDATE_ID );
    wxCommandEvent eventStop( wxEVT_COMMAND_TEXT_UPDATED, THREAD_STOPPED_ID );

    // Generate event indicating that thread has started    
    m_parent->GetEventHandler()->AddPendingEvent(eventStart);

    for(int x = 0; x < 64; x++)
    {
        // Add new value to external array at index x
        *(ucExternDataArray + x) = (unsigned char)x;
        
        // Add to event the index (in external array) of the new data
        eventNewData.SetInt(x);
        
        // Generate event indicating new data available
        m_parent->GetEventHandler()->AddPendingEvent( eventNewData );

        this->Sleep(500);
        if(TestDestroy())
            x = 64;      
    }

    // Generate event indicating that thread is stopping    
    m_parent->GetEventHandler()->AddPendingEvent(eventStop);
    
    return 0;
}
Windows XP, wxDev-C++ 7.4.2.259, wxWidgets 2.8.12, MingW

User avatar
doublemax
Moderator
Moderator
Posts: 14314
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxThread using pointer to change array data in main thre

Post by doublemax » Sun Jan 13, 2013 11:39 am

As long as the lifetime of the array is long enough, it's no problem. Eventually you might need to protect access to the array with a wxMutex, so that only the main thread or the secondary thread can access it at a time. Whether this is necessary in this case, depends on the actual task.

Code: Select all

wxFrame* m_parent;
BTW: Usually it's better to just store the wxEvtHandler pointer, because that's all you need and it ensures that you don't accidentally call a GUI method of wxFrame.
Use the source, Luke!

softport
Experienced Solver
Experienced Solver
Posts: 61
Joined: Sat Jan 28, 2012 3:55 pm
Location: Houston TX
Contact:

Re: wxThread using pointer to change array data in main thre

Post by softport » Sun Jan 13, 2013 11:58 am

Thanks doublemax! The array lasts for the duration of the program.

Had a thought just after posting, will this work in reverse? Say after creation of the new thread, I pass the main thread a pointer to some data in the new thread. Could the main thread send messages to the new thread this way? I don't know if I will need this, just curious, and too tired to try it right now.

Must sleep...

Just noticed your suggestion, at the moment I'm not sure what you mean about storing the pointer. It's not safe as a private member of the thread?
Last edited by softport on Sun Jan 13, 2013 12:02 pm, edited 1 time in total.
Windows XP, wxDev-C++ 7.4.2.259, wxWidgets 2.8.12, MingW

User avatar
doublemax
Moderator
Moderator
Posts: 14314
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxThread using pointer to change array data in main thre

Post by doublemax » Sun Jan 13, 2013 12:01 pm

Theoretically possible, but i would try to avoid this situation. As the lifetime of the secondary thread is always shorter than the main thread, you must take precautions that the main thread doesn't access the array after the thread has been destroyed. Especially if it's a detached thread that destroys itself.
Use the source, Luke!

softport
Experienced Solver
Experienced Solver
Posts: 61
Joined: Sat Jan 28, 2012 3:55 pm
Location: Houston TX
Contact:

Re: wxThread using pointer to change array data in main thre

Post by softport » Sun Jan 13, 2013 12:04 pm

Right, forgot about that. Thanks again!
Windows XP, wxDev-C++ 7.4.2.259, wxWidgets 2.8.12, MingW

Post Reply