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