#include <WScom.h>
#include <WSCfunctionList.h>
#include <WSCbase.h>
#include <WSDthread.h>
#include <WSDsemaphore.h>
#include <stdlib.h>
#include <unistd.h>
//----------------------------------------------------------
//Function for the event procedure
//----------------------------------------------------------
#include <newwin000.h>
WSDsemaphore* sem = NULL;
static long cnt=0;

//running as the sub thread.
void* thread_func(WSDthread* obj,void* val){
  sem->lock(); //wait for the semaphore
  while(1){
    obj->execCallback((void*)cnt);
#ifdef MSW
    Sleep(1000);
#else
    sleep(1);
#endif
    cnt++;
  }
  return NULL;
}


//running on the main thread.
void callback_func(WSDthread*,void* val){
  long cnt = (long)val;
  //display the counter
  newvbtn_000->setProperty(WSNlabelString,cnt);  
}

//running under the main thread.
void btnop(WSCbase* object){
  //do something...
  if (sem == NULL){
    sem = WSDsemaphore::getNewInstance();
    sem->initialize();
    sem->lock();
    WSDthread* thr = WSDthread::getNewInstance();
    thr->setFunction(thread_func);
    thr->setCallbackFunction(callback_func);
    thr->createThread((void*)0);
    object->setProperty(WSNlabelString,"Start Thread");
  }else{
    sem->unlock(); //start the waiting thread.
  }
}
static WSCfunctionRegister  op("btnop",(void*)btnop);
