wxwidget structure understanding

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.
paddle
Knows some wx things
Knows some wx things
Posts: 27
Joined: Sat Oct 12, 2019 4:57 pm

Re: wxwidget structure understanding

Post by paddle » Thu Oct 31, 2019 5:20 am

Technically you don't need the protection for atomic variables like an integer.
Got it thanks!

Then what variable would need this protection? How far goes the atomic variables? Would I be fine using a struct with this kind of data directly?

Code: Select all

typedef struct GlobalMem
{
	std::array<long long, 5> ArrayOfInt
	sqlite3 *db;
	std::vector<std::array<long long, 5> > VectorOfArrayOfLonglong;

	std::vector<long long> VectorOfLongLong;
	tree<long long>Tree;
	tree<long long>::iterator treePointeur, TreeRoot, TreeTrunk;

	int sleep;

} globalMem;

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

Re: wxwidget structure understanding

Post by doublemax » Thu Oct 31, 2019 6:23 am

AFAIK sqlite3 is thread safe. But for all other containers except char/short/int/long etc. you need the critical section.

But you don't have to add methods for all variables, you can also just lock the whole structure with a critical section.
Use the source, Luke!

paddle
Knows some wx things
Knows some wx things
Posts: 27
Joined: Sat Oct 12, 2019 4:57 pm

Re: wxwidget structure understanding

Post by paddle » Thu Oct 31, 2019 6:47 am

doublemax wrote:
Thu Oct 31, 2019 6:23 am
But you don't have to add methods for all variables, you can also just lock the whole structure with a critical section.
How do you apply a critical section to the whole class or structure?

Another noob question : I don't understand the syntax of the constructor. What introduce the ":" ? I guess it's to apply parameters of the template class from which XxxFrame is derived? But then what the "," doing?
Sorry for the noob question but I'm having trouble googling this.

Code: Select all

XxxFrame::XxxFrame(const wxString& title): wxFrame(NULL, wxID_ANY, title), m_thread( &m_thread_status )
On the wxSmith template the constructor begins with

Code: Select all

XxxFrame::XxxFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(MemBFrame)
    wxMenu* Menu1;
...
I fear if I just do

Code: Select all

XxxFrame::XxxFrame(wxWindow* parent,wxWindowID id) : wxFrame(NULL, wxID_ANY, title), m_thread( &m_thread_status )
{
    //(*Initialize(MemBFrame)
    wxMenu* Menu1;
...
There might be some issue as the XxxFrame parameters are not the same to begin with.

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

Re: wxwidget structure understanding

Post by doublemax » Thu Oct 31, 2019 8:01 am

What introduce the ":" ? I guess it's to apply parameters of the template class from which XxxFrame is derived? But then what the "," doing?
The

Code: Select all

: wxFrame(NULL, wxID_ANY, title)
calls the constructor of the base class

Code: Select all

, m_thread( &m_thread_status )
initializes a member variable

Code: Select all

XxxFrame::XxxFrame(wxWindow* parent,wxWindowID id)
wxSmith doesn't call the constructor of the base class, so the default constructor will be used implicitly. But afterwards it calls Create(...) to actually create the frame.

You can also just initialize m_thread in code inside the constructor:

Code: Select all

m_thread = &m_thread_status;
Use the source, Luke!

paddle
Knows some wx things
Knows some wx things
Posts: 27
Joined: Sat Oct 12, 2019 4:57 pm

Re: wxwidget structure understanding

Post by paddle » Thu Oct 31, 2019 11:38 am

doublemax wrote:
Thu Oct 31, 2019 8:01 am
You can also just initialize m_thread in code inside the constructor:

Code: Select all

m_thread = &m_thread_status;
This bit of code isn't working though. Tried to add it to minimal and it can't build

Code: Select all

// frame constructor
MyFrame::MyFrame(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title)
{
	m_thread = &m_thread_status;
I think there's a typo there, m_thread cannot be initialized as m_thread_status right?

doublemax wrote:
Thu Oct 31, 2019 6:23 am
But you don't have to add methods for all variables, you can also just lock the whole structure with a critical section.
How would you apply a critical section to the whole class in your demo below?

Code: Select all

class ThreadStatus
{
public:
  ThreadStatus()
  {
  };

  void SetInteger( int value ) {
    wxCriticalSectionLocker lock(m_cond);
    m_int = value;
  };

  void SetString( const wxString &value ) {
    wxCriticalSectionLocker lock(m_cond);
    // force deep copy of string data
    m_string = wxString( value.c_str() );
  };

  int GetInteger() {
    wxCriticalSectionLocker lock(m_cond);
    return m_int;
  }

  wxString GetString() {
    wxCriticalSectionLocker lock(m_cond);
    return m_string;
  }

protected:
  int m_int;
  wxString m_string;
  wxCriticalSection m_cond;
};

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

Re: wxwidget structure understanding

Post by doublemax » Thu Oct 31, 2019 11:59 am

I think there's a typo there, m_thread cannot be initialized as m_thread_status right?
Yeah, sorry about that.

In that case you'll need to add a default constructor for DemoThread and a new method to set the pointer for the ThreadStatus.
How would you apply a critical section to the whole class in your demo below?
You need one wxCriticalSection instance somewhere (it could also be in the struct itself) and lock/unlock it manually when accessing the data.
Use the source, Luke!

Post Reply