wxThreads and update of common variable 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
totobond
Earned a small fee
Earned a small fee
Posts: 11
Joined: Mon Oct 02, 2006 10:01 am

wxThreads and update of common variable

Post by totobond »

Hi

I've an easy question but as I've never done such kind of code, I don't know how to do that.
I've a list of threads doing a specific treatment and each of them needs to insert an item (inside a wxlistctrl that is in the mainframe).
My problem is the following:
Each thread may insert an item at the same unexpected time but InsertItem() method of wxListCtrl will fail if index of item to be created already exist.
Thread1 inserts item1, item2 and Thread2 inserts also the item1, ...

I was thinking on a wxSemaphore to protect the number of already created items in the wxlistctrl (then when a thread will insert an item, we are sure the item doesn't exist) but I'm not sure it is the best solution

Any idea ?

Antonio
tim.ebenezer
Earned a small fee
Earned a small fee
Posts: 19
Joined: Sun Feb 18, 2007 11:45 pm

Post by tim.ebenezer »

hi Antonio
In wxListCtrl, can't you just create the wxListItem item first - and add that straight without an index?

Code: Select all

wxListItem item();
item.SetText(wxT("String to be inserted"));
listCtrl->InsertItem(item);
If that isn't viable - for whatever reason - you could aways do something like:

Code: Select all

long l;
int index = 0;
do {
 l = listCtrl->InsertItem(index, wxT("String to be inserted"));
 index++;
} while (l != -1)
Obviously the first is far nicer.

Tim
--
iBook G4 - OS X 10.4 - wxMac 2.8.0 - Compiling for Universal - g++

Toshiba Portege - 1.4 Ghz Centrino - Windows XP - wxMSW 2.8.0 - mingw
eg0master
In need of some credit
In need of some credit
Posts: 5
Joined: Mon Feb 19, 2007 7:57 am
Location: Stockhom, Sweden
Contact:

Post by eg0master »

No, please... the "hacky" solutions suggested above might work but a synchronization object is definitly the right thing to do. However there are small sublimal differences in how they work and in your case you should use wxMutex or wxCriticalSection. Probably the latter is your best bet.
totobond
Earned a small fee
Earned a small fee
Posts: 11
Joined: Mon Oct 02, 2006 10:01 am

Post by totobond »

Hi guys

Thanks for your proposed solutions...
I'm going to try both solutions even if I think a synchronized solution is the best.

I will give you result after test.

Regards

Antonio
totobond
Earned a small fee
Earned a small fee
Posts: 11
Joined: Mon Oct 02, 2006 10:01 am

Post by totobond »

Hi

I tested both proposals and it works.
My final solution is to post an event to my wxListCtrl (belonging to my wxFrame)

thanks
Post Reply