_beginThreadex创建多线程解读

简介: _beginThreadex创建多线程解读一、需要的头文件支持 #include <process.h> // for _beginthread()需要的设置:ProjectàSetting-->C/C++-->User run-time library 选择Debug Multithreaded 或者Multithreaded。即使用: MT或MTD。

源码如下:

#include <stdio.h>

#include <string>             // for STL string class

#include <windows.h>          // for HANDLE

#include <process.h>          // for _beginthread()

using namespace std;

 

class ThreadX

{

private:

 int loopStart;

 int loopEnd;

 int dispFrequency;

public:

 string threadName;

 

 ThreadX( int startValue, int endValue, int frequency )

 {

loopStart = startValue;

loopEnd = endValue;

dispFrequency = frequency;

 }

 

 static unsigned __stdcall ThreadStaticEntryPoint(void * pThis)

 {

  ThreadX * pthX = (ThreadX*)pThis;   // the tricky cast

  pthX->ThreadEntryPoint();           // now call the true entry-point-function

  return 1;            // the thread exit code

 }

 

 void ThreadEntryPoint()

 {

for (int i = loopStart; i <= loopEnd; ++i)

{

  if (i % dispFrequency == 0)

  {

   printf( "%s: i = %d\n", threadName.c_str(), i );

  }

}

printf( "%s thread terminating\n", threadName.c_str() );

 }

};

 

 

int main()

{

   ThreadX * o1 = new ThreadX( 0, 1, 2000 );

 

   HANDLE   hth1;

   unsigned  uiThread1ID;

 

   hth1 = (HANDLE)_beginthreadex( NULL,         // security

                                  0,            // stack size

                                  ThreadX::ThreadStaticEntryPoint,

                                  o1,           // arg list

                                  CREATE_SUSPENDED,  // so we can later call ResumeThread()

                                  &uiThread1ID );

 

   if ( hth1 == 0 )

       printf("Failed to create thread 1\n");

 

   DWORD   dwExitCode;

   GetExitCodeThread( hth1, &dwExitCode );  // should be STILL_ACTIVE = 0x00000103 = 259

   printf( "initial thread 1 exit code = %u\n", dwExitCode );

 

   o1->threadName = "t1";

 

   ThreadX * o2 = new ThreadX( -100000, 0, 2000 );

 

   HANDLE   hth2;

   unsigned  uiThread2ID;

 

   hth2 = (HANDLE)_beginthreadex( NULL,         // security

                                  0,            // stack size

                                  ThreadX::ThreadStaticEntryPoint,

                                  o2,           // arg list

                                  CREATE_SUSPENDED,  // so we can later call ResumeThread()

                                  &uiThread2ID );

 

   if ( hth2 == 0 )

       printf("Failed to create thread 2\n");

 

   GetExitCodeThread( hth2, &dwExitCode );  // should be STILL_ACTIVE = 0x00000103 = 259

   printf( "initial thread 2 exit code = %u\n", dwExitCode );

 

   o2->threadName = "t2";

 

   ResumeThread( hth1 );   // serves the purpose of Jaeschke's t1->Start()

   ResumeThread( hth2 );    

 

   WaitForSingleObject( hth1, INFINITE );

   WaitForSingleObject( hth2, INFINITE );

 

   GetExitCodeThread( hth1, &dwExitCode );

   printf( "thread 1 exited with code %u\n", dwExitCode );

 

   GetExitCodeThread( hth2, &dwExitCode );

   printf( "thread 2 exited with code %u\n", dwExitCode );

 

   CloseHandle( hth1 );

   CloseHandle( hth2 );

 

   delete o1;

   o1 = NULL;

 

   delete o2;

   o2 = NULL;

 

   printf("Primary thread terminating.\n");

return 0;

}

1.


相关文章
|
7月前
|
安全 Java 程序员
Java多线程(1)---多线程认识、四种创建方式以及线程状态
Java多线程(1)---多线程认识、四种创建方式以及线程状态
71 0
|
7月前
多线程学习(二) 多线程创建4种方式
多线程学习(二) 多线程创建4种方式
37 0
|
6月前
|
Java API 调度
多线程的操作
多线程的操作
29 0
|
8月前
|
Java
创建多线程的方式四:使用线程池
创建多线程的方式四:使用线程池
38 0
|
8月前
|
调度 双11
多线程的创建方法--多线程基础(一)
线程为一个"执行流". 每个线程之间都可以按照自己的顺序执行.
|
10月前
|
存储 Java C++
多线程的2种实现方式
多线程的2种实现方式
79 0
|
Java API
Java开发——35.多线程_(线程的创建及使用)
线程创建方式:1.继承Thread类;2.实现Runnable接口。
Java开发——35.多线程_(线程的创建及使用)
多线程之常见方法使用
线程是程序执行的最小单位,而进程是操作系统分配资源的最小单位;进程是系统资源分配的单位,线程是系统调度的单位。一个进程由一个或多个线程组成,线程是一个进程中代码的不同执行路线;进程之间相互独立,进程之间不能共享资源,而线程共享所在进程的地址空间和其它资源。同时线程还有自己的栈和栈指针,程序计数器等寄存器。
|
Java 调度
【多线程:常见方法】
【多线程:常见方法】
82 0
|
调度
【多线程:一些方法的注意点】
【多线程:一些方法的注意点】
86 0