Delegate task to another thread - Help 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.
jfouche
Super wx Problem Solver
Super wx Problem Solver
Posts: 442
Joined: Tue May 06, 2008 4:52 pm
Location: France

Post by jfouche »

evstevemd wrote:I'm interested in this function
void wxThreadEvent::SetPayload ( const T & payload ) [inline]

Sets custom data payload.

The payload argument may be of any type that wxAny can handle (i.e. pretty much anything). Note that T's copy constructor must be thread-safe, i.e. create a copy that doesn't share anything with the original (see Clone()).
does wxAny handle resultsets from wxSQLite3?
wxSQLite3ResultSet define a copy constructor, but I'm not sure it's thread safe. Looking to the sources :

Code: Select all

wxSQLite3ResultSet::wxSQLite3ResultSet(const wxSQLite3ResultSet& resultSet)
{
  m_db = resultSet.m_db;
  m_stmt = resultSet.m_stmt;
  // Only one object can own the statement
  const_cast<wxSQLite3ResultSet&>(resultSet).m_stmt = 0;
  m_eof = resultSet.m_eof;
  m_first = resultSet.m_first;
  m_cols = resultSet.m_cols;
  m_ownStmt = resultSet.m_ownStmt;
}
I think YES. Ask Utell to be sure.

EDIT : But don't use the result after you posted it in another thread, because the copy of the result set will set the statement to NULL.
Jérémie
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Post by evstevemd »

jfouche wrote: EDIT : But don't use the result after you posted it in another thread, because the copy of the result set will set the statement to NULL.
I don't understand that! What do you mean by "don't use the result after you posted it in another thread"
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
jfouche
Super wx Problem Solver
Super wx Problem Solver
Posts: 442
Joined: Tue May 06, 2008 4:52 pm
Location: France

Post by jfouche »

If you post this event from the thread A to the main thread, there will be a copy of the resultset class. If you copy it, the sqlite statement will be available in the new class (eg : in the event). The resultset will be empty in the thread A : you can't use it anymore.

Code: Select all

wxThreadEvent e(...);
e.Setpayload( resultSet );
m_handler->AddPendingEvent( e );
// Don't use the result set now
// The sqlite statement is NULL
Jérémie
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Post by evstevemd »

jfouche wrote:If you post this event from the thread A to the main thread, there will be a copy of the resultset class. If you copy it, the sqlite statement will be available in the new class (eg : in the event). The resultset will be empty in the thread A : you can't use it anymore.

Code: Select all

wxThreadEvent e(...);
e.Setpayload( resultSet );
m_handler->AddPendingEvent( e );
// Don't use the result set now
// The sqlite statement is NULL
I see! I will not use it anymore as thread will have served it purpose. Thanks!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Post by evstevemd »

Thanks everybody,
I just made it! Your posts have helped me and I have learned a lot from you guys. Here is Codelite project with all files I was tinkering with. I hope someone will brush the code well and post to Wiki(there is no wxThreadEvent working example)
Attachments
Threading.tar.gz
(7.01 KiB) Downloaded 106 times
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Post Reply