multi thread in PhAB

Hi, I would like to run a simple multi threaded program in Photon using PhAB. In my GUI I have two buttons where one button is to lunch thread1 and the other button is to lunch thread2. The task of thread 1 and 2 is to display number from 1 to 100 in two seperate label box.
There is the code:

thread1.c
/* Y o u r D e s c r i p t i o n /
/
AppBuilder Photon Code Lib /
/
Version 2.03 */

/* Standard headers /
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
/
Local headers */
#include “ablibs.h”
#include “abimport.h”
#include “proto.h”

void * t1()
{
int i;
char buf[10];
for (i=0;i<100;i++)
{
sprintf(buf,"%d",i);
PtSetResource(ABW_Thread1_display,Pt_ARG_TEXT_STRING,buf,0);
PtFlush();
PtBkgdHandlerProcess();
usleep(100000);
}
pthread_exit(NULL);
}

int
thread1( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )

{
int thread_id;
pthread_t p_thread;
pthread_attr_t attr;
pthread_attr_init(&attr);
/* eliminate ‘unreferenced’ warnings */
widget = widget, apinfo = apinfo, cbinfo = cbinfo;
thread_id=pthread_create(&p_thread,&attr,t1,NULL);
pthread_join(p_thread,NULL);
return( Pt_CONTINUE );
}

Thread2.c

/* Y o u r D e s c r i p t i o n /
/
AppBuilder Photon Code Lib /
/
Version 2.03 */

/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

/* Local headers */
#include “ablibs.h”
#include “abimport.h”
#include “proto.h”

void * t2()
{
int i;
char buf[10];

for (i=0;i<100;i++)
{
sprintf(buf,"%d",i);
PtSetResource(ABW_Thread2_display,Pt_ARG_TEXT_STRING,buf,0);
PtFlush();
PtBkgdHandlerProcess();
usleep(100000);
}
pthread_exit(NULL);
}

int
thread2( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )

{
int thread_id;
pthread_t p_thread;
pthread_attr_t attr;
pthread_attr_init(&attr);
/* eliminate ‘unreferenced’ warnings */
widget = widget, apinfo = apinfo, cbinfo = cbinfo;
thread_id=pthread_create(&p_thread,&attr,t2,NULL);
pthread_join(p_thread,NULL);
return( Pt_CONTINUE );
}

When I press button1 it runs thread1.c and works fine.
but when I press button2 it blocks thread1 and runs thread2.
and only when thread2 finishes running it runs thread1 again.
Is using PtEnter() and PtLeave a key here?
if so can you suggest a fix. Thanks

What you are doing is illegal. Photon is not thread safe. Yes you need to use PtEnter/PtLeave.

I assume you are doing this as an experience or something of that nature. Starting a thread just to change a resource is bad design.

How do you tell whith thread is running when?

yes, I am doing this as an experiment for my actual design. I am trying to start one thread to read from Camera1 and display the picture on one part of the screen by a press of a button. And by pressing another button, I would like the second thread to read from camera2 and also display the picture on the screen in parallel with thread1.

And I don’t really care which thread runs as long as they get a fair share of the CPU. Let’s say I would like thread1 to run for 1ms and switch control to thread2 for 1ms and back and forth.

Thanks

Since Photon is not multithreaded, there is generaly no gain in having multiple thread doing rendering. It makes the design more complicated and harder to debug.

You can setup a background or timer handler which does

if ( button1 pressed )
display from first camera
if ( button 2 pressed)
display from second camera.

You should be able to do this faster then the camera can grab the image, unless it’s a high speed camera.

In a loop

thanks, I am trying with the RtTimerCreate().

As Wojtek said in the newsgroup. Have threads that do all the processing is fine, the threads could be doing the grab and do processing on the image, but the display should be done by the main thread only.

Personnaly I would put the control of the grabbing in a separate process. The photon app would send a message when to start/stop grabbing and the grabbing process could put the image in share memory.

It’s a lot more modular this way.

It is done this way. I have a resource manager which receives the command and puts the data out to a shared memory. From my PhAB app, all I am doing is sending devctl() message to the Resource manager and waiting for the data to be filled in the shared memory. Once the data is ready, my PhAB app will read from the shared memory and fill an PhImage_t variable “image”. I then call PgDrawPhImage() call to draw this “image” to the screen. And I wanted to the PhAB app to send devctl() request to resource manager and update the screen using threads to get data from two camera. I was putting sleep() command in side the thread to control the devctl() read request, but I guess I should be doing this using Timer. The only problem is that Timer is not hard real-time, and so how can I make this work? After getting the thread version to work, I see now that it’s not a clean design to use threads in PhAB.

I would use a notification mecanism. The Photon apps sends a request for X number of frames to the grabber resmgr. When the grabber has an image ready in memory (images can even be queued) it notifies the Photon applicationof the available data. Each camera would gets it own notify data (a pulse per camera for example).

When the Photon app is notify it would go and draw the image as fast as it can. No need for timer or polling. Photon not being thread safe each draw operation is done one at a time, there is no point in If there is a need to grab image at certain rate, make that a feature part of the grabber resmgr. Add a command that specify number of frame and frame rate and let the resmgr deal with real time issue. The resmgr could validate these numbers, if the camera is 120Hz and gui is asking frame rate of 150Hz, there is a problem. Let the driver be smart.

Asking GUI to be real-time is bad design.