PtSetResources and threads

Hi ^^

I’m running this code from a callback attached to a button:

[code]int
draw_poly(PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackinfo_t *cbinfo)
{
PhPoint_t *points;
PhPoint_t orig;
PtArg_t args[2];
int i;

/* eliminate 'unreferenced' warnings */
widget = widget, apinfo = apinfo, cbinfo = cbinfo;

// while (1) {

points = (PhPoint_t *) malloc (181 * sizeof(PhPoint_t));
for (i = 0; i < 181; i++)
{
	points[i].x = (short) (200 * cos(i * 0.01745));
	points[i].y = (short) -(200 * sin(i * 0.01745));
}
orig.x = 200;
orig.y = 0;
PtSetArg(&args[0], Pt_ARG_ORIGIN, &orig, 1);
PtSetArg(&args[1], Pt_ARG_POINTS, points, 181);
PtSetResources(ABW_RangeDisplay, 2, args);

// sleep(1); }

return(Pt_Continue);

}
[/code]

It runs pretty well and draws a nice 180° arc.
Now suppose I want to make it a thread. I turn it into a void function, remove the return(Pt_Continue), and change the callback function to call it using pthread_create. Then it still works, no problem.
Ok, now suppose I want to make it redraw every one second. I uncomment the commented lines, and then it will work once, but then crash.

My question is: why ? And how to avoid it ? (ok that’s 2 questions ^^ )[/code]

Put PtEnter() before your Photon function calls, and PtLeave() after them, Photon is not thread safe and those two functions block calls to Photon from other threads, making Photon calls safe to use in threads.

I tried this before but it didn’t work :-/
I found this in the documentation:

So I guess I should use a timed callback for updating the display instead of a thread…

If u want to use PtEnter()/PtLeave() then u would have to implement them into all threads using Photon calls. Also, as u mentioned, calling PtSetResources() doesnt actually change display, it only schedule the update on next event-handle cycle. So, after calling PtSetResources() in second thread u have to let the first thread (the one which called PtMainLoop()) to update display.

Single thread with a Timer Callback is somewhat better solution …

Understood !
It’s working now (with a timer)