基于C++实现的线程休眠代码

本文实例讲述了基于C++实现的线程休眠代码,分享给大家供大家参考。具体方法如下:

linux平台示例如下:

/*
File   : thread1.c
Author  : Mike
E-Mail  : Mike_Zhang@live.com
*/
#include <stdio.h>
#include <pthread.h>
#include <time.h>
void m_threadSleep(int sec,int nsec)
{
  struct timespec sleepTime;
  struct timespec returnTime;
  sleepTime.tv_sec = sec;
  sleepTime.tv_nsec = nsec;
  nanosleep(&sleepTime, &returnTime);
}
void test1()
{
  m_threadSleep(1,0);
  printf("I'm thread1 ...\r\n");
}
void test2()
{
  m_threadSleep(2,0);
  printf("I'm thread2 ...\r\n");
}
int main()
{
  pthread_t thread1,thread2;
  void *result;
  time_t tbegin,tend;
  tbegin = time(NULL);
  pthread_create(&thread1,NULL,(void*)&test1,NULL);
  pthread_create(&thread2,NULL,(void*)&test2,NULL);
  pthread_join(thread1,&result);
  pthread_join(thread2,&result);
  tend = time(NULL);
  printf("%d\r\n",tend-tbegin);
  return 0;
}

编译代码如下:

gcc thread1.c -o thread1 -lpthread

boost库实现示例如下:

/*
File   : boost_thread1.cpp
Author  : Mike
E-Mail  : Mike_Zhang@live.com
*/
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp>
#include <iostream>

boost::xtime getSleepTime(int sec,int nsec)
{
  boost::xtime t;
  boost::xtime_get(&t, boost::TIME_UTC);
  t.sec += sec;
  t.nsec += nsec;
  return t;
}
void test1()
{
  boost::this_thread::sleep(getSleepTime(1,500));
  std::cout <<"I'm thread1 !"<< std::endl;
}
void test2()
{
  boost::this_thread::sleep(getSleepTime(3,500));
  std::cout <<"I'm thread2 !"<< std::endl;
}

int main(int argc, char* argv[])
{
  boost::thread thrd1(&test1);
  boost::thread thrd2(&test2);
  std::time_t t_begin,t_end;
  t_begin = time(NULL);
  thrd1.join();
  thrd2.join();
  t_end = time(NULL);
  std::cout<<t_end-t_begin<<std::endl;
  return 0;
}

编译命令如下:

g++ boost_thread1.cpp -o boost_thread1 -lboost_thread-mt

希望本文所述对大家的C++程序设计有所帮助。

(0)

相关推荐

  • C++线程同步实例分析

    本文实例分析了C++线程同步问题,分享给大家供大家参考.具体分析如下: 该实例设置全局变量g_bContinue,在主线程中设置全局变量g_bContinue,工作线程检测该全局变量,实现主线程控制工作线程的目的. 打印出的g_cnt1与g_cnt2的数值不同,是因为线程调试时时间片的切换. 具体代码如下: // countError.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> DWORD

  • C++开发:为什么多线程读写shared_ptr要加锁的详细介绍

    我在<Linux 多线程服务端编程:使用 muduo C++ 网络库>第 1.9 节"再论 shared_ptr 的线程安全"中写道: (shared_ptr)的引用计数本身是安全且无锁的,但对象的读写则不是,因为 shared_ptr 有两个数据成员,读写操作不能原子化.根据文档(http://www.boost.org/doc/libs/release/libs/smart_ptr/shared_ptr.htm#ThreadSafety), shared_ptr 的线程

  • C/C++ 多线程的学习心得总结

    个人觉得在学习多线程编程之前最好先了解进程和线程的关系, 然后在学习线程工作方式的过程中动手写个(我是从抄开始的)多线程的小程序, 会对学习多线程有很大的帮助, 否则只有理论是很抽象的. 在学习多线程编程之前, 必须先知道什么是 线程函数, 线程函数就是另一个线程的入口函数. 默认情况下一个我们所写的代码都是只有一个线程的, 而这个线程的入口函数就是main() 函数, 这是系统默认的. 而我们创建的另一个线程也需要一个函数来进入, 这个函数就叫做线程函数. 在C/C++中, 可以调用 '运行期

  • 解析C/C++中如何终止线程的运行

    想要终止线程的运行,可以使用以下方法: 1.线程函数返回(最好使用该方法). 2.通过调用ExitThread函数,线程将自行撤消(最好不使用该方法). 3.同一个进程或另一个进程中的线程调用TerminateThread函数(应避免使用该方法). 4.ExitProcess和TerminateProcess函数也可以用来终止线程的运行(应避免使用该方法). 下面将详细介绍终止线程运行的方法:1-4,并说明线程终止运行时会出现何种情况:5. 1.线程函数返回 始终都应该将线程设计成这样的形式,即

  • C++采用TLS线程局部存储的用法实例

    本文实例讲述了C++采用TLS线程局部存储的用法.分享给大家供大家参考. 具体方法如下: 复制代码 代码如下: // useTLS.cpp : 定义控制台应用程序的入口点.  //    #include "stdafx.h"  #include <Windows.h>  #include <process.h>    //声明  VOID InitStartTime();  DWORD GetUserTime();    //TLS索引,作全局变量   DWO

  • c++版线程池和任务池示例

    commondef.h 复制代码 代码如下: //单位秒,监测空闲列表时间间隔,在空闲队列中超过TASK_DESTROY_INTERVAL时间的任务将被自动销毁const int CHECK_IDLE_TASK_INTERVAL = 300;//单位秒,任务自动销毁时间间隔const int TASK_DESTROY_INTERVAL = 60; //监控线程池是否为空时间间隔,微秒const int IDLE_CHECK_POLL_EMPTY = 500; //线程池线程空闲自动退出时间间隔

  • C++设置事件通知线程工作的方法

    本文实例讲述了C++设置事件通知线程工作的方法,其中主线程通过将事件状态设置为"受信"来通知工作线程工作.具体实现方法如下: 复制代码 代码如下: // eventDemo.cpp : 定义控制台应用程序的入口点.  //    #include "stdafx.h"  #include <Windows.h>  #include <process.h>  HANDLE g_event;    UINT __stdcall ThreadPro

  • C++使用CriticalSection实现线程同步实例

    本文实例讲述了C++使用CriticalSection实现线程同步的方法,在前文C++线程同步实例分析的基础上增加了四行代码,使用了四个函数: EnterCriticalSection ::DeleteCriticalSection ::EnterCriticalSection ::LeaveCriticalSection此时,打印出来的数字就相等了. 具体代码如下: #include "stdafx.h" #include <Windows.h> DWORD g_cnt1

  • C++线程池的简单实现方法

    本文以实例形式较为详细的讲述了C++线程池的简单实现方法.分享给大家供大家参考之用.具体方法如下: 一.几个基本的线程函数: 1.线程操纵函数: int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, (void*)(*start_rtn)(void *), void *arg); //创建 void pthread_exit(void *retval); //终止自身 int pthread_cancel(pthread_

  • C/C++中退出线程的四种解决方法

    退出线程可以有四种方法: 1.线程函数的return返回(最好这样):其中用线程函数的return返回, 而终止线程是最安全的, 在线程函数return返回后, 会清理函数内申请的类对象, 即调用这些对象的析构函数. 然后会自动调用 _endthreadex()函数来清理 _beginthreadex(...)函数申请的资源(主要是创建的tiddata对象). 2.调用 _endthreadex()函数 或 ExitThread()函数(最好不要):如果使用这两种方法退出线程, 则不会执行线程函

  • C++线程优先级SetThreadPriority的使用实例

    本文实例讲述了C++线程优先级SetThreadPriority的使用方法,分享给大家供大家参考.具体方法如下: 复制代码 代码如下: // ThreadPriority.cpp : 定义控制台应用程序的入口点.  //    #include "stdafx.h"  #include <Windows.h>    DWORD WINAPI ThreadProcIdle(LPVOID lpParameter)  {      for (int i=0;i<20;i++

  • C++实现CreatThread函数主线程与工作线程交互的方法

    本文实例讲述了C++开启线程CreatThread函数的使用,实现主线程与工作线程交互的功能.分享给大家供大家参考. 具体实现代码如下: 复制代码 代码如下: //线程函数  DWORD WINAPI ThreadProc(LPVOID lpParameter)  {      for (int i=0;i<20;i++)      {          printf("I'm in thread,count=%d\n",i);      }      return 0;  } 

随机推荐