Python on BeOS

Donn Cave donn at drizzle.com
Wed Jun 27 23:19:53 EDT 2001


Quoth "Harvest T. Moon" <info at black-karma.de>:
| "Donn Cave" <donn at drizzle.com> schrieb
|> Bethon is not a GUI module, and it sure is thread-safe, but indeed
|> it is wrappers for the native BeOS API.  What did you do, that led
|> you to think it was not thread-safe?
|
| hum, maybe it's my way to work with threads. but i.e. in a listbox:
| everytime i tried to insert an item to it from some other thread than the
| mainthread the application crashes. i'll look up the code and post it later
| the day :)

Say no more.  If I am right about what you're doing, I can reproduce
the crash - in Python, or in C++.

Before you modify data structures associated with a thread, you have
to lock it.  Here, that means you must invoke the Lock() function of
the BWindow that draws the BListView, then call BListView::AddItem(),
and then BWindow::Unlock().

Or, I really strongly prefer to post a message to the BWindow's
message dispatching loop.  Both the window and the view have
message handlers and can receive the message, depending on your
preference.  It can be awkward in a way, but as your program
gets larger and more complex this technique doesn't get any
harder to deal with, whereas the perils of locking grow to the
point of certain disaster.  What if a function invoked from
the window's message dispatch loop eventually calls the
function that adds the list item?  Lock() will hang forever.

To call a function with non-trivial arguments, you can set
the data aside somewhere and let the other thread come and
get it;  here, interlocking is safe and desirable.  To get
a more synchronous execution model, than the default, you
can supply a reply message in BMessenger::Send().

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list