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

//running as the sub thread.
void* thread_func(WSDthread* obj,void*){
  while(1){
    mutex->lock();
    cnt++;
    obj->execCallback((void*)cnt);
#ifdef MSW
    Sleep(1000);
#else
    sleep(1);
#endif
    mutex->unlock();
  }
  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 on the main thread.
void btnop(WSCbase* object){
  //do something...
  if (mutex == NULL){
    //create mutex.
    mutex = WSDmutex::getNewInstance();
    mutex->initialize();
    //create 10 threads to count it up.
    long i;
    for(i=0; i<10; i++){
      WSDthread* thr = WSDthread::getNewInstance();
      thr->setFunction(thread_func);
      thr->setCallbackFunction(callback_func);
      thr->createThread((void*)0);
    }
  }else{
    exit(0);
  }
}
static WSCfunctionRegister  op("btnop",(void*)btnop);
