Thread compare

พูดคุย, ปรึกษาการพัฒนาโปรแกรม
และ case study

Moderator: bigaun

Thread compare

Postby nat3 » Fri Nov 24, 2006 8:14 am

เอาโค้ดเกี่ยวกับ Thread ในภาษาต่างๆ มาให้ดูเผื่ออยากเขียนกัน
ต้นฉบับ : http://www.codeproject.com/threads/ThreadClass.asp
Java
Code: Select all
// define class with a threadable method
public class MyObject implements Runnable {
  // the thread procedure
  public void run() {
    // TODO: put the code here
  }
}

MyObject obj = new MyObject();
Thread thread = new Thread(obj);
tread.start();

.net
Code: Select all
// define class with a threadable method
public class MyObject {
  // the thread procedure
  public void Run() {
    // TODO: put the code here
  }
}

MyObject obj = new MyObject();
Thread thread = new Thread(new ThreadStart(obj.Run));
tread.Start();

ข้อแตกต่างระหว่าง .net กับ java คงเป็นที่ object ของ .net เป็น thread อยู่แล้วกระมัง สามารถเรียกใช้ function ย่อยได้เลย ดังนั้น เราสามารถกำหนด thread ว่าให้รันใน procedure ไหนของ object ได้โดยที่ไม่ต้องสร้าง class แยก ต่างจากจาวา หากต้องการสร้าง procedure ที่จะให้รัน ต้องกำหนดเป็น if then elf หรือ สร้างเป็นคลาสแยกไป ดูตัวอย่างด้านล่างดีก่า
.net
Code: Select all
// create a threadable object
public class MyObject {
  // first thread procedure
  public void ThreadProc1() {
    // TODO:   
  }
  // second thread procedure
  public void ThreadProc2() {
    // TODO:
  }
}

MyObject obj = new MyObject();

// create first thread
Thread thread1 = new Thread( new ThreadStart(obj.ThreadProc1) );
thread1.Start();

//create second thread
Thread thread2 = new Thread( new ThreadStart(obj.ThreadProc2) );
thread2.Start();

มันจะสะดวกสะบายอะไรเช่นนี้นี่ .net (แต่เขียนไม่เป็นง่ะ ไว้ต้องหัดเขียนมั่งและ)
มาดู C++ มั่งดีกว่า เพื่อให้ง่ายสร้าง Interface แบบ Java ก่อน (แต่ C++ ไม่มี Interface หนิ ก็สร้างเป็น คลาสเลยละกัน)

Code: Select all
// define the interface
struct IRunnable {
  virtual void run() = 0;
};

// define the thread class
class Thread {
public:
  Thread(IRunnable *ptr) {
    _threadObj = ptr;
  }
  void start() {
    // use the Win32 API here
    DWORD threadID;
    ::CreateThread(0, 0, threadProc, _threadObj, 0, &threadID);
  }
 
protected:
  // Win32 compatible thread parameter and procedure
  IRunnable *_threadObj;
  static unsigned long __stdcall threadProc(void* ptr) {
    ((IRunnable*)ptr)->run();
    return 0;
  }   
};


จากนั้นเวลาใช้ก็ตามด้านล่าง

Code: Select all
// define class with a threadable method
class MyObject : IRunnable {
public:
  // the thread procedure
  virtual void run() {
    // TODO: put the code here
  }
}

MyObject *obj = new MyObject();
Thread thread = new Thread(obj);
tread->start();


ถ้าจะทำแบบ .net
Code: Select all
// define class with a threadable method
class MyObject : IRunnable {
// pointer to a class method
typedef void (MyObject::* PROC)();
PROC fp;

// first thread procedure
void threadProc1() {
   //TODO: code for this thread procedure
}
// second thread procedure
void threadProc2() {
   //TODO: code for this thread procedure
}
   
public:
  MyObject() {
    fp = threadProc1;
  }
  void setThreadProc(int n) {
    if(n == 1)
        fp = threadProc1;
    else
    if(n == 2)
        fp = threadProc2;
  }
  // the thread procedure
  virtual void run() {
    (this->*fp)();
  }
};

MyObject *obj = new MyObject();

obj->setThreadProc(1);
Thread *thread1 = new Thread(obj);
thread1->start();

obj->setThreadProc(2);
Thread *thread2 = new Thread(obj);
thread2->start();
User avatar
nat3
Moderators
 
Posts: 1229
Joined: Mon Jun 13, 2005 12:52 pm

Postby bigaun » Wed Nov 29, 2006 11:30 am

สาระดีจริงๆ
Aun # 17
User avatar
bigaun
Moderators
 
Posts: 2022
Joined: Wed Jun 22, 2005 3:13 pm
Location: Live in webboard

Postby nat3 » Fri Dec 01, 2006 5:52 pm

ต่อไปว่าจะเอา socket กะ mpi นะเนี๊ยะ แต่ดูแล้ว คนดูมันโหวงเหวงเหลือเกิน เอามาลงแล้วรู้สึกน้อยใจนิดๆ ไม่มีคนดู เห้อ ไปอัพเดทใส่บล๊อกตัวเองดีกว่า
User avatar
nat3
Moderators
 
Posts: 1229
Joined: Mon Jun 13, 2005 12:52 pm

Postby Poron » Sun Dec 03, 2006 12:26 am

มันคืออารายยังมะรู้เลย
บิ๊กกี้คุง #21
see me @ BiggyClub

Image
User avatar
Poron
Member
 
Posts: 609
Joined: Sun May 28, 2006 2:17 am
Location: http://biggyclub.co.cc


Return to Programming / Web-Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron