Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > Создание потоков на Builder 6


Автор: Sergio 28.1.2007, 19:39
Здраствуйте. Читал книгу Рихтера по потокам и вот столкнулся с ними на практике. Что-то не очень выходит smile Нужно запихнуть в поток вот этот вечный цикл и запустить поток в мейне:
Код

  while(1)
   {
    int x = x++;
   }


Вот как я делаю:
Код

 DWORD WINAPI MyThread(LPVOID lpParam)
 {
  while(1)
   {
    int x = x++;
   }
  return 0;
 }



Код

int main(int argc, char* argv[])
{
  DWORD res;
 CreateThread(NULL,0,ClientThread, ???, 0, res);
}

Я не понял что в парамете должно быть там где знаки вопроса smile 
Зарание спасибо.

Автор: Romikgy 28.1.2007, 19:55
CreateThread(NULL,0,ClientThread, NULL , 0, res);
Цитата

The CreateThread function creates a thread to execute within the address space of the calling process. 

HANDLE CreateThread(

    LPSECURITY_ATTRIBUTES lpThreadAttributes,    // pointer to thread security attributes  
    DWORD dwStackSize,    // initial thread stack size, in bytes 
    LPTHREAD_START_ROUTINE lpStartAddress,    // pointer to thread function 
    LPVOID lpParameter,    // argument for new thread 
    DWORD dwCreationFlags,    // creation flags 
    LPDWORD lpThreadId  // pointer to returned thread identifier 
   );    
 

Parameters

lpThreadAttributes

Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpThreadAttributes is NULL, the handle cannot be inherited. 

Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new thread. If lpThreadAttributes is NULL, the thread gets a default security descriptor. 
Windows 95: The lpSecurityDescriptor member of the structure is ignored.

dwStackSize

Specifies the size, in bytes, of the stack for the new thread. If 0 is specified, the stack size defaults to the same size as that of the primary thread of the process. The stack is allocated automatically in the memory space of the process and it is freed when the thread terminates. Note that the stack size grows, if necessary. 
CreateThread tries to commit the number of bytes specified by dwStackSize, and fails if the size exceeds available memory.

lpStartAddress

The starting address of the new thread. This is typically the address of a function declared with the WINAPI calling convention that accepts a single 32-bit pointer as an argument and returns a 32-bit exit code. Its prototype is:

DWORD WINAPI ThreadFunc( LPVOID );

lpParameter

Specifies a single 32-bit parameter value passed to the thread. 

dwCreationFlags

Specifies additional flags that control the creation of the thread. If the CREATE_SUSPENDED flag is specified, the thread is created in a suspended state, and will not run until the ResumeThread function is called. If this value is zero, the thread runs immediately after creation. At this time, no other values are supported.

lpThreadId

Points to a 32-bit variable that receives the thread identifier. 

или юзай класс TThread

Автор: Sergio 28.1.2007, 20:01
Спасибо.

Автор: nworm 28.1.2007, 20:01
Это 
Код

LPVOID lpParam

который передаётся в функцию 
Код

 MyThread(LPVOID lpParam)


А, ещё, надо поправить
Код

CreateThread(NULL,0,MyThread, ???, 0, res);

Автор: Ustus 29.1.2007, 20:04
Кстати, здесь есть одна проблема, как сказано в help'е:
Код

A thread that uses functions from the C run-time libraries should use the beginthread and endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called. 

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)