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


Автор: shad0w 27.2.2006, 02:03
Здраствуйте. Есть ли у кого доки на ету тему?

Автор: kirjanov 27.2.2006, 09:33
http://users.actcom.co.il/~choo/lupg/tutorials/multi-thread/multi-thread.html - POSIX потоки

Автор: AlexKar 1.3.2006, 22:46
Читай книгу Джеффри РИХТЕРА "Cоздание эффективных WIN32-приложений", там все подробно описано с примерами на C.

Автор: chipset 1.3.2006, 23:31
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/multiple_threads.asp

Автор: TeeT 2.3.2006, 17:54
Вот Кусок из Хелпа:
Цитата

Header File

process.h

Category

Process Control Routines

Prototype

unsigned long _beginthreadex(void *__security_attr, unsigned __stksize, unsigned (__stdcall *__start)(void *), void *__arg, unsigned __create_flags, unsigned *__thread_id);

Description

Creates a thread and allows specifying the other parameters of the OS API CreateThread (such as security and thread creation flags). The _endthreadex function will be called automatically when the thread function terminates. The value returned from your thread function will be passed along to _endthreadex, which in turn will pass it along to the ExitThread API. The return value can then be retrieved using the GetExitCodeThread API.

Unlike _endthread, the _endthreadex function does not close the thread handle, thereby allowing other threads to block on this one without fear that the handle will be freed out from under the system.

Other than the order of parameters and the closing of the thread handle, _beginthreadex performs same operation as _beginthreadNT.

Note: The start address needs to be defined to return an unsigned, which is the thread exit code.

Return Value

_beginthreadex returns the handle of the new thread. The return value is a standard Windows handle that can be used in operating system API's such as SuspendThread and ResumeThread.

If unsuccessful, -1 is returned, and  errno is set as follows:

Error Constants Description

EAGAIN Too many threads
ENOMEM Not enough memory
EINVAL Bad stack value (i.e. less than 16 bytes or equal to zero)

Also see the description of the Win32 API GetLastError, in the MSDN Library.


Экзампл:
Код

//*  Use the -tWM  (32-bit multi-threaded target) command-line switch for this example  */
#pragma checkoption -tWM
#include        <windows.h>
#include        <process.h>
#include        <stdio.h>

#define NTHREADS 25

/* This function acts as the 'main' function for each new thread */
static unsigned __stdcall threadMain(void *arg)
{
   printf("Thread %2d has an ID of %u\n", (int)arg, GetCurrentThreadId());
   return 0;
}

int main(void)
{
   HANDLE hThreads[NTHREADS];

   int i;
   unsigned threadId;
   SECURITY_ATTRIBUTES  sa = {
          sizeof(SECURITY_ATTRIBUTES), /* structure size */
          0,      /* No security descriptor */
          TRUE    /* Thread handle is inheritable */
   };

   /* Create NTHREADS inheritable threads that are initially suspended and that will run starting at threadMain()*/
   for(i = 0; i < NTHREADS; i++) {

      hThreads[i] = (HANDLE)_beginthreadex(
         &sa,          /* Thread security */

         4096,         /* Thread stack size */
         threadMain,   /* Thread starting address */
         (void *)i,    /* Thread start argument */
         CREATE_SUSPENDED,  /* Create in suspended state */
         &threadId);   /* Thread ID */
      if(hThreads[i] == INVALID_HANDLE_VALUE) {
         MessageBox(0, "Thread Creation Failed", "Error", MB_OK);
         return  1;
      }
      printf("Created thread %2d with an ID of %u\n", i, threadId);
   }
   printf("\nPress ENTER to thaw all threads\n\n");

   getchar();
   
/* Resume the suspended threads */
   for(i = 0; i < NTHREADS; i++)
      ResumeThread(hThreads[i]);
   
/* Wait for the threads to run */
   WaitForMultipleObjects(NTHREADS, hThreads, TRUE, INFINITE);

/* Close all of the thread handles */
   for(i = 0; i < NTHREADS; i++)
      CloseHandle(hThreads[i]);

   return 0;
}

Автор: Janus 3.3.2006, 20:07
А кроссплатформенный вариант? В паскале есть BeginThread() и подобные, а в С++ что0нибудь наподобие?

Автор: Void 3.3.2006, 21:41
Цитата(Janus @ 3.3.2006, 22:07 Найти цитируемый пост)
А кроссплатформенный вариант?

Кажется, кроме http://www.boost.org/libs/thread/doc/index.html ничего не придумали. Может быть, еще POSIX-потоки через Cygwin… утверждать не буду.
Цитата(Janus @ 3.3.2006, 22:07 Найти цитируемый пост)
В паскале есть BeginThread() и подобные

Пардон, с какого боку эта функция кросслплатформенна? Это же Delphi вроде?

Автор: Janus 4.3.2006, 20:36
Это FreePascal, вообще-то. В Delphi такого отродясь не было, разве что с winapi "притащить".

Автор: threef 5.3.2006, 14:59
стандартная библиотека С , семейство функций spawn, зависит от реализации компилятора. В свое время только TopSpeed C поддерживал реализацию многозадачности. В самом языке многозадачности нет. Большинство используют поддержку API операционки - то, что описано выше.

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