ANTICHAT.XYZ    VIDEO.ANTICHAT.XYZ    НОВЫЕ СООБЩЕНИЯ    ФОРУМ  
Баннер 1   Баннер 2
Antichat снова доступен.
Форум Antichat (Античат) возвращается и снова открыт для пользователей. Здесь обсуждаются безопасность, программирование, технологии и многое другое. Сообщество снова собирается вместе.
Новый адрес: forum.antichat.xyz
Вернуться   Форум АНТИЧАТ > Оффтоп > Forum for discussion of ANTICHAT
   
Ответ
 
Опции темы Поиск в этой теме Опции просмотра

Creating a Thread with a method as a startproc
  #1  
Старый 06.06.2006, 19:00
Аватар для Dracula4ever
Dracula4ever
Постоянный
Регистрация: 08.05.2006
Сообщений: 559
Провел на форуме:
1593567

Репутация: 354


Отправить сообщение для Dracula4ever с помощью ICQ Отправить сообщение для Dracula4ever с помощью AIM Отправить сообщение для Dracula4ever с помощью Yahoo
Post Creating a Thread with a method as a startproc

Here is something I've picked up while trying to open a new thread for a method, and not a function

CreateThread only accepts a ThreadProc (http://msdn.microsoft.com/library/de...threadproc.asp) as a start routine. of course, specifying object.method as a start routine won't work, a call to a method is not the same as to a function...
But, a call to a static function is very much like it, so we just make our method static and we can specify it as parameter for CreateThread

"What's the point using a static method, I have lost all my object properties"
well, here is another trick, you can literally "fake" the 'this' pointer and you can get all your nonstatic properties and methods

how do you do that, simple, the CreateThread function allows you to pass one parameter to the start routine, so you can pass the object in hand and get back all the data you 'lost'

In case all of this is a bit mixed up, here is an example:
//'Start' would be the trigger for the new thread that will run on 'bar':
class foo{
private:
int a=0;
static DWORD WINAPI bar(LPVOID lpParam){
foo *_this=(foo *)lpParam; //We can do this because we know that the param is always a foo instance

//now we can use _this as our 'this' the same way
cout << _this->a << endl;
return 0;
}
public:
void Start(){
int thread_id;
CreateThread(NULL, 0,
this->listen_thread, //We CAN send here a static method, we CANNOT send a nonstatic one
this, //In order to later fake 'this'
0, &thread_id);
}
}


enjoy!
dracula4eever
 
Ответить с цитированием
Ответ



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Удобный Http брут на Php KEZ PHP, PERL, MySQL, JavaScript 18 13.08.2005 13:34



Здесь присутствуют: 1 (пользователей: 0 , гостей: 1)
 


Быстрый переход




ANTICHAT.XYZ