Như các bạn đã biết, hàm CreateThread cần biết địa chỉ của hàm mà nó sẽ thực thi khi thread chạy(The starting address for a thread) và dĩ nhiên hàm này phải là hàm tỉnh(static function), bạn phải khai báo static nếu là class member hoặc khai báo bên ngoài class như C style.
Ví dụ:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//.......... | |
CreateThread(NULL, 0, thread_proc, NULL, 0, &threadId); | |
//.......... | |
DWORD thread_proc(LPVOID lpParameter) | |
{ | |
printf("background thread!"); | |
} |
Cách làm thì có nhiều nhưng do trình độ có hạn nên chỉ dám đóp góp 2 ngu ý như sau:
Cách đầu tiên
Như ta đã biết, hàm CreateThread cho phép ta pass 1 đối số là con trỏ kiểu void(con trỏ mất dạy nhất, nó có thể là bất cứ con trỏ nào). Ta sẽ pass chính con trỏ của thực thể class cần thực thi hàm non-static vào CreateThread và sau đó nhận lại con trỏ này tại đối số của ThreadProc, bước tiếp theo ta chỉ cần gọi hàm non-static cần thực thi từ con trỏ này.Ý tưởng như sau:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass | |
{ | |
public: | |
// hàm này sẽ đc thực thi trong thread | |
void run_bg_thread() | |
{ | |
printf("Background thread!"); | |
} | |
}; | |
//.......... | |
MyClass myClass; | |
DWORD threadId; | |
CreateThread(NULL, 0, thread_proc, &myClass, 0, &threadId); | |
//.......... | |
DWORD thread_proc(LPVOID lpParameter) | |
{ | |
MyClass * myClass = (MyClass *) lpParameter; | |
myClass->run_bk_thread(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass | |
{ | |
// phương thức này sẽ đc thực thi trong thread | |
void run_bg_thread() | |
{ | |
printf("Background thread!"); | |
} | |
static DWORD thread_proc(LPVOID lpParameter) | |
{ | |
MyClass * myClass = (MyClass *) lpParameter; | |
myClass->run_bg_thread(); | |
} | |
public: | |
void start_thread() | |
{ | |
CreateThread(NULL, 0, thread_proc, this, 0, &threadId); | |
} | |
}; | |
// khi dùng chỉ cần viết đơn giản như sau | |
MyClass myClass; | |
myClass.start_thread(); |
Cách thứ 2
Cách này cao siêu hơn tí, đầu tiên ta sẽ định nghĩa 1 abstract class(đúng hơn là interface nhưng trong C++ nó ko có khái niệm này) chứa 1 public method kiểu pure virtual. Sau đó các lớp nào muốn chạy trong thread thì cần kế thừa từ class này. Chúng ta sẽ tìm hiểu cụ thể ngay bên dưới.Đầu tiên là abstract class đóng vai trò như là 1 interface:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// giống java ko ![]() |
|
class Runnable | |
{ | |
public: | |
virtual void run() = 0;// pure virtual method | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Task | |
{ | |
public: | |
static void run(Runnable * runnable); | |
}; | |
void Task::run(Runnable * runnable) | |
{ | |
DWORD threadId; | |
CreateThread(NULL, 0, [](LPVOID p)->DWORD { ((Runnable *)p)->run(); return 0; }, runnable, 0, &threadId); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass: public Runnable | |
{ | |
public: | |
virtual void run() | |
{ | |
printf("Background thread!"); | |
} | |
}; | |
MyClass myClass; | |
Task::run(&myClass); |

0 nhận xét :
Post a Comment