File: thread.cpp - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off


#include <iostream>
#include "windows.h"
#include "process.h"

//#define TEST_CRT
#define TEST_WINAPI

#ifdef TEST_CRT
  #define TEST_FUNC_CREATE(c) (HANDLE)_beginthreadex (NULL, 0, (c), 0, CREATE_SUSPENDED, NULL)
  #define TEST_FUNC_DELETE(h) ({ if ((h)) { WaitForSingleObject ((h), INFINITE); CloseHandle ((h)); }})
  #define TEST_FUNC_RESUME(h) ({ if ((h)) ResumeThread ((h)); })
  #define TEST_RETURN     unsigned int __stdcall
#endif

#ifdef TEST_WINAPI
  #define TEST_FUNC_CREATE(c) CreateThread (NULL, 0, (c), 0, CREATE_SUSPENDED, NULL)
  #define TEST_FUNC_DELETE(h) ({ if ((h)) { WaitForSingleObject ((h), INFINITE); CloseHandle ((h)); }})
  #define TEST_FUNC_RESUME(h) ({ if ((h)) ResumeThread ((h)); })
  #define TEST_RETURN     DWORD WINAPI
#endif

struct  Ex
{
};

TEST_RETURN MyCallback (void*)
{
  try
  {
    throw Ex ();
  }
  catch (Ex&)
  {
  }

  return 0;
}

const int count = 100;
const int loop = 10;

int main()
{
  HANDLE  threads[count];
  int   i;
  int   j;

  for (i = 0; i++ < loop; )
  {
    std::cout << "Iteration #" << i << std::endl;

    std::cout << "    Create threads..." << std::endl;

    for (j = 0; j++ < count; )
      threads[j] = TEST_FUNC_CREATE (&MyCallback);

    Sleep (500);

    std::cout << "    Resume theads..." << std::endl;

    for (j = 0; j++ < count; )
      TEST_FUNC_RESUME (threads[j]);

    Sleep (500);

    std::cout << "    Delete threads..." << std::endl;

    for (j = 0; j++ < count; )
      TEST_FUNC_DELETE (threads[j]);

    Sleep (500);

    std::cout << "    Done." << std::endl;
  }

  return 0;
}