Page 2 of 2

Re: wxwidget structure understanding

Posted: Thu Oct 31, 2019 5:20 am
by paddle
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;

Re: wxwidget structure understanding

Posted: Thu Oct 31, 2019 6:23 am
by doublemax
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.

Re: wxwidget structure understanding

Posted: Thu Oct 31, 2019 6:47 am
by paddle
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.

Re: wxwidget structure understanding

Posted: Thu Oct 31, 2019 8:01 am
by doublemax
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;

Re: wxwidget structure understanding

Posted: Thu Oct 31, 2019 11:38 am
by paddle
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;
};

Re: wxwidget structure understanding

Posted: Thu Oct 31, 2019 11:59 am
by doublemax
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.