WideStudio/MWT Logo
WideStudio/MWT
Programming Guide
WideStudio/MWT Index
Table of contents


简易Thread/semaphore/Mutex



利用简易Thread类来生成Thread

利用WSDthread 类的话,可从程序上生成Thread。

btnop()函数是押下按钮时被运行的事件过程实体。 在该事件过程上,将生成 WSDthread 类的Instance,并定义实行函数thread_func()为一个Thread,然后生成Thread。 同时,通过作为Thread动作中的 thread_func()函数,利用execCallback()成员函数来向Main Thread通知事件活动状态。 execCallback()成员函数被调用时,在Main Thread上,被setCallbackFunction()成员函数所指定了的Callback函数将动作,Thread和Main Thread将进行同期处理。

#include <WScom.h>
#include <WSCfunctionList.h>
#include <WSCbase.h>
//----------------------------------------------------------
//Function for the event procedure
//----------------------------------------------------------
WSDthread* thr = NULL;

//将运行的函数作为Sub Thread进行定义

void* thread_func(WSDthread* obj,void* ptr){
  long cnt=(long)ptr;
  while(1){
    cnt++;
    obj->execCallback((void*)cnt); // 通知Main Thread 
#ifdef MSW
    Sleep(1000);
#else
    sleep(1);
#endif
  }
  return NULL;
}

//获取Thread来的通知,在Main Thread运行Callback
void callback_func(WSDthread*,void* val){
long cnt =(long)val;
  //表示被追加的内容。
  newvbtn_000->setProperty(WSNlabelString,cnt);
}

//事件过程。在按下按钮时运行。
void btnop(WSCbase* object){
  if (thr == NULL){
    thr = WSDthread::getNewInstance();
    thr->setFunction(thread_func);
    thr->setCallbackFunction(callback_func);
    thr->createThread((void*)100);
  }else{
    exit(0);
  }
}

由createThread()成员函数启动的Thread,因是作为Main Thread的异步Sub Thread而动作,所以不能对Thread un-safe的WideStudio 的 GUI Instance进行非排他控制。 需要通过WideStudio 的 GUI Instance显示Thread的处理结果等情况时,对Thread Instance调用execCallback()成员函数、 利用通知Main Thread的方法,在Main Thread上表示处理结果。

Mutex

利用Mutex,可对复数的Thread进行排他处理,对于同时来的处理,只有一个Thread有效。 下例是在由简易Thread生成的复数Thread运行中,采用Mutex实现排他处理的例子。


#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;

//定义作为复数Sub Thread进行运行的函数

void* thread_func(WSDthread* obj,void*){
  while(1){
  mutex->lock();
  cnt++;
  obj->execCallback((void*)cnt); //通知Main Thread
#ifdef MSW
    Sleep(1000);
#else
    sleep(1);
#endif
    mutex->unlock();
  }
  return NULL;
}

//获取由Thread来的通知,在Main Thread上运行Callback
void callback_func(WSDthread*,void* val){
  long cnt =(long)val;

  //表示被追加的内容。
  newvbtn_000->setProperty(WSNlabelString,cnt);
}

//事件过程。在按下按钮时开始运行。
void btnop(WSCbase* object){
  //do something...
  if (mutex == NULL){
    //生成实现排他处理的Mutex
    mutex = WSDmutex::getNewInstance();
    mutex->initialize();

    //同时生成10个Thread,逐渐追加数值
    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);



semaphore

利用semaphore,可实现与Mutex同样的排他处理,或对复数的Thread运行协调动作的等候处理。 下列的例子,是对由简易Thread生成了的Thread,利用semaphore运行等候处理的例子。第一次按下按钮时,生成Thread,利用semaphore让Thread待命。当第2次按下按钮时,解除semaphore,使待命的Thread开始动作。

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

//作为Thread动作的函数。

void* thread_func(WSDthread* obj,void* val){
  sem->lock();  //通过semaphore开始待命。
  while(1){
    obj->execCallback((void*)cnt);
#ifdef MSW
    Sleep(1000);
#else
    sleep(1);
#endif
    cnt++;
  }
  return NULL;
}

//获取由Thread来的通知,在Main Thread上运行的Callback
void callback_func(WSDthread*,void* val){
  long cnt =(long)val;
  //表示被追加的内容。
  newvbtn_000->setProperty(WSNlabelString,cnt);
}

//事件过程。在按下按钮时开始运行。
void btnop(WSCbase* object){
  //do something...
  if (sem == NULL){
    //制作semaphore。
    sem = WSDsemaphoree::getNewInstance();
    sem->initialize();
    sem->lock();

    //生成Tread。
    WSDthread* thr = WSDthread::getNewInstance();
    thr->setFunction(thread_func);
    thr->setCallbackFunction(callback_func);
    thr->createThread((void*)0);
    object->setProperty(WSNlabelString,"Start Thread");
  }else{
    //第2次运行时解除semaphore,让等候的Thread开始动作。
    sem->unlock();  //start the waiting thread.
  }
}
static WSCfunctionRegister op("btnop",(void*)btnop);


Document Release 3.90 for WideStudio/MWT ver 3.90, Jul 2005


WideStudio/MWT documents index | Table of contents

Copyright(C) WideStudio/MWT Development Team, 1999-2005 Last modified: Jul 31, 2005