⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sbtrd.cpp

📁 sloedgy open sip stack source code
💻 CPP
📖 第 1 页 / 共 2 页
字号:
   // Use _beginthreadex( ) so that the CRT (common runtime
   // environment) is fully initialized so all CRT functions work,
   // CreateThread( ) doesn't do this. However, this is Microsoft Visual C++
   // specific, not part of the official Win32 API.
   typedef unsigned int (__stdcall *beginthreadfunc)(void *);
   beginthreadfunc func = reinterpret_cast<beginthreadfunc>(thread_function);
   void *arg = static_cast<void *>(thread_arg);
 
   result->threadHandle = 
     reinterpret_cast<HANDLE>(_beginthreadex(NULL, g_threadStackSize, 
                              func, arg, 0, &result->threadID));
   if (result->threadHandle == 0) {
     delete result;
     return VXItrd_RESULT_FATAL_ERROR;
   }
 
   *thread = result;
   return VXItrd_RESULT_SUCCESS;
 }
 
 
 /**
  * Destroy a thread handle
  *
  * Note: this does NOT stop or destroy the thread, it just releases
  * the handle for accessing it. If this is not done, a memory leak
  * occurs, so if the creator of the thread never needs to communicate
  * with the thread again it should call this immediately after the
  * create if the create was successful.
  *
  * @param  thread  Handle to the thread to destroy
  *
  * @result VXItrdResult 0 on success 
  */
 VXITRD_API VXItrdResult VXItrdThreadDestroyHandle(VXItrdThread **thread)
 {
   if ((! thread) || (! *thread)) return VXItrd_RESULT_INVALID_ARGUMENT;
 
   CloseHandle ((*thread)->threadHandle);
   delete *thread;
   *thread = NULL;
   return VXItrd_RESULT_SUCCESS;
 }
 
 
 /**
  * Purpose Terminate a thread.  Called by the thread on exit.
  *
  * @param  status  Exit code for the thread
  * @result           N/A, never returns
  */
 VXITRD_API void VXItrdThreadExit(VXItrdThreadArg status)
 {
   _endthreadex(reinterpret_cast<unsigned int>(status));
 }
 
 
 /**
  * Causes the calling thread to wait for the termination of a specified
  *   'thread' with a specified timeout, in milliseconds. 
  *
  * @param   thread the 'thread' that is waited for its termination.
  * @param   status contains the exit value of the thread's start routine.
  * @return  VXItrdResult of operation.  Return SUCCESS if specified 'thread' 
  *          terminating.
  */
 VXITRD_API VXItrdResult VXItrdThreadJoin(VXItrdThread *thread,
 					 VXItrdThreadArg *status,
 					 long timeout)
 {
   if ((thread == NULL ) || (status == NULL) || (timeout < -1))
     return VXItrd_RESULT_INVALID_ARGUMENT;
 
   if (timeout == -1)
     timeout = INFINITE;
 
   VXItrdResult rc;
   switch (WaitForSingleObject(thread->threadHandle, timeout)) {
   case WAIT_OBJECT_0:
   case WAIT_ABANDONED:
     rc = VXItrd_RESULT_SUCCESS;
     break;
   case WAIT_TIMEOUT:
     rc = VXItrd_RESULT_FAILURE;
     break;
   default:
     rc = VXItrd_RESULT_FATAL_ERROR;
   }
 
   DWORD stat;
   if (rc == VXItrd_RESULT_SUCCESS) {
     if (GetExitCodeThread(thread->threadHandle, &stat) == 0)
       rc = VXItrd_RESULT_FATAL_ERROR;
     else
       *status = reinterpret_cast<VXItrdThreadArg>(stat);
   }
 
   return rc;
 }
 
 
 /**
  * Get the thread ID for the specified thread
  *
  * @param  thread   Handle to the thread to get the ID for
  *
  * @result   Thread ID number
  */
 VXITRD_API VXIthreadID VXItrdThreadGetIDFromHandle(VXItrdThread *thread)
 {
   if (thread == NULL) 
     return (VXIthreadID) -1;
   return thread->threadID;
 }
 
 
 /**
  * Purpose Get the ID of the current handle.
  *
  * @return  The current thread handle identifier.
  *
  */
 VXITRD_API VXIthreadID VXItrdThreadGetID(void)
 {
   return GetCurrentThreadId();
 }
 
 
 /**
  * Purpose Yield the process schedule in the current thread.
  *
  * @return  void
  *
  */
 VXITRD_API void VXItrdThreadYield(void)
 {
   Sleep(0);
 }
 
 
 /**
  * Purpose  Create a timer
  *
  * @param   mutex  a pointer to a mutex
  * @return  VXItrdResult of operation.  Return SUCCESS if timer has been 
  *          created
  *
  */
 VXITRD_API VXItrdResult VXItrdTimerCreate(VXItrdTimer **timer)
 {
   if (timer == NULL) return VXItrd_RESULT_INVALID_ARGUMENT;
   
   *timer = NULL;
   
   // Create the wrapper object
   VXItrdTimer *result = new VXItrdTimer;
   if (result == NULL)
     return VXItrd_RESULT_OUT_OF_MEMORY;
 
   // Create the timer, a Win32 event
   result->timerEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
   if (result->timerEvent == NULL) {
     delete result;
     return VXItrd_RESULT_FATAL_ERROR;
   }
 
   *timer = result;
   return VXItrd_RESULT_SUCCESS;
 }
 
 
 /**
  * Purpose  Destroy a timer
  *
  * @param   timer  a pointer to a timer
  * @return  VXItrdResult of operation.  Return SUCCESS if timer has been 
  *          created
  *
  */
 VXITRD_API VXItrdResult VXItrdTimerDestroy(VXItrdTimer **timer)
 {
   if ((! timer) || (! *timer)) return VXItrd_RESULT_INVALID_ARGUMENT;
 
   if (CloseHandle((*timer)->timerEvent) == FALSE) 
     return VXItrd_RESULT_FATAL_ERROR;
 
   delete *timer;
   *timer = NULL;
   return VXItrd_RESULT_SUCCESS;
 }
 
 
 /**
  * Purpose  Puts the current thread to sleep for a configurable duration.
  *          Due to other activities of the machine, the delay is the minimum
  *          length that the timer will wait.
  *
  * @param   timer  a pointer to a timer
  * @param   millisecondDelay  the minimum number of milliseconds to wait
  * @param   interrupted  a pointer (may optionally be NULL) indicating whether
  *            or not the sleep was interrupted by VXItrdTimerWake.
  * @return  VXItrdResult of operation.  Return SUCCESS if timer could sleep.
  *
  */
 VXITRD_API VXItrdResult VXItrdTimerSleep(VXItrdTimer *timer,
 					 VXIint millisecondDelay,
 					 VXIbool *interrupted)
 {
   if ((timer == NULL) || (millisecondDelay < 0)) 
     return VXItrd_RESULT_INVALID_ARGUMENT;
 
   switch (WaitForSingleObject(timer->timerEvent, millisecondDelay)) {
   case WAIT_OBJECT_0:
     if (interrupted != NULL) *interrupted = 1;
     break;
   case WAIT_TIMEOUT:
     if (interrupted != NULL) *interrupted = 0;
     break;
   default:
     return VXItrd_RESULT_FATAL_ERROR;
   }
 
   return VXItrd_RESULT_SUCCESS;
 }
 
 
 /**
  * Purpose  Wakes a sleeping thread, if the target is not already sleeping
  *          it immediately wakes when it tries to sleep the next time.
  *
  * @param   timer  a pointer to a timer
  * @return  VXItrdResult of operation.  Return SUCCESS if timer could wake.
  *
  */
 VXITRD_API VXItrdResult VXItrdTimerWake(VXItrdTimer *timer)
 {
   if (timer == NULL) return VXItrd_RESULT_INVALID_ARGUMENT;
 
   if (SetEvent(timer->timerEvent) == 0) return VXItrd_RESULT_FATAL_ERROR;
 
   return VXItrd_RESULT_SUCCESS;
 }
 
 
 /**
  * Initialize the TRD utilities library
  *
  * @param threadStackSize  Stack size to use when creating new threads.
  *                         Pass 0 to use the default (OS-specific) size,
  *                         usually this means new threads will use the
  *                         same stack size as the main (parent) process.
  *
  * @return VXItrd_RESULT_SUCCESS if sucess, different value on failure
  */
 VXITRD_API VXItrdResult VXItrdInit (VXIint32 threadStackSize)
 {
   if (threadStackSize >= 0)
     g_threadStackSize = threadStackSize;
   return VXItrd_RESULT_SUCCESS;
 }
 
 
 /**
  * Shutdown the TRD utilities library
  *
  * @return VXItrd_RESULT_SUCCESS if sucess, different value on failure
  */
 VXITRD_API VXItrdResult VXItrdShutDown ()
 {
   return VXItrd_RESULT_SUCCESS;
 }
 
#endif


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -