Java 语言,把并发机制包含在核心语言中,因为Java语言具有平台无关性。而C没有并发机制,C++标准中也因此并没有纳入并发机制。我们在windows平台下开发c++,当使用并发机制时往往使用的SDK win32 api。在《c++编程思想》一书中,作者提供了一种开源的跨平台的高级面向对象的线性和sycnchronization 库 -- ZThread 。

ZThread库的主页:http://zthread.sourceforge.net

上面的网站打不开。可以用下面到地址来下载。

源码Zthread下载地址: http://prdownloads.sourceforge.net/zthread/ZThread-2.3.2.tar.gz

一、在linux 下编译

1.下载保存 文件到 /usr/local/src

2. 解压

cd /usr/local/src

tar -zxvf ZThread-2.3.2.tar.gz

3.编译安装

cd ZThread-2.3.2

./configure --prefix=/usr/local/ZThread

如果在./configure时遇到错误:

error: C++ compiler cannot create executables

yum install gcc-c++

然后继续:

make

在make时遇到错误:

../include/zthread/Guard.h: In destructor 'ZThread::Guard<LockType, LockingPolicy>::~Guard()':

../include/zthread/Guard.h:494: error: there are no arguments to 'isDisabled' that depend on a template parameter, so a declaration of 'isDisabled' must be available

只需 先export CXXFLAGS=-fpermissive,然后再执行

./configure --prefix=/usr/local/ZThread

make

make install

二、linux下使用

1.写文件      zthread0.h

#ifndef LIFT_OFF_H
#define LIFT_OFF_H
#include <iostream>
#include "zthread/Runnable.h"
using namespace std;class LiftOff : public ZThread :: Runnable{int countdown;int id;
public:LiftOff(int count,int ident=0):countdown(count),id(ident){}~LiftOff(){cout << id << "completed " << endl;}void run(){while(countdown--){cout << id << ":" << countdown << endl; }cout << id << " lift off " << endl;}
};
#endif

BasicThreads.cpp

#include "zthread0.h"
#include "zthread/Thread.h"
using namespace std;
using namespace ZThread;int main(){try{Thread t(new LiftOff(10),true);cout << "Waiting for LiftOff " << endl;}catch(Synchronization_Exception& e){cerr << e.what() << endl;}
} ///:~

2.编译链接加入ZThread库

 gcc BasicThreads.cpp -L /usr/local/ZThread/lib -I /usr/local/ZThread/include  -lstdc++ -lZThread

注意 -L 后面接ZThread的安装路径/lib

要加入-lstdc++,否则会有如下错误:

(.text+0x3a29): undefined reference to `std::basic_ostream<char, std::char_traits<char>

要加入-lZThread,相当于加入附加依赖项ZThread.lib,否则会有如下错误:

(.text+0xd1):XX.cpp: undefined reference to `ZThread::Thread::Thread(ZThread::Task const&, bool)'

3.运行

./a.out

可能会有错误:error while loading shared libraries: libZThread-2.3.so.2: cannot open shared object file: No such file or directory

将libZThread-2.3.so.2拷贝到/usr/lib下

如: cp /usr/local/ZThread/lib/libZThread-2.3.so.2 /usr/lib

然后运行OK ,输出如下所示:

Waiting for LiftOff 0:9

0:8
0:7
0:6
0:5
0:4
0:3
0:2
0:1
0:0
0 lift off
0completed

在linux下使用ZThread相关推荐

  1. 过滤Linux下不同大小的文件,linux查找当前目录下 M/G 大小的文件,删除Linux下指定大小的文件

    过滤Linux下不同大小的文件,linux查找当前目录下 M/G 大小的文件,删除Linux下指定大小的文件 find ./ -type f -size +1G| xargs rm 在清理系统日志文件 ...

  2. Linux下创建硬链接,文件访问为空,提示:xxxx: 符号连接的层数过多

    Linux下创建软链接|硬链接,文件访问为空,提示:x x x: 符号连接的层数过多. 原因:创建符号链接的时候未使用绝对路径,无论是源文件路径还是目标路径,都需要使用绝对路径. 如: ln -s / ...

  3. Linux下环境变量配置方法梳理(.bash_profile和.bashrc的区别)

    博客园 首页 新随笔 联系 管理 订阅 <div class="blogStats"><!--done--> 随笔- 556  文章- 38  评论- 77 ...

  4. linux下yum错误:[Errno 14] problem making ssl connection Trying other mirror.

    所有的base 都要取消注释 mirrorlist 加上注释 另外所有的enable都要设为零 目录 今天是要yum命令安装EPEL仓库后 yum install epel-release 突然发现y ...

  5. linux下使用source /etc/profile保存配置后,新的环境变量只能在一个终端里面有效

    博客园 首页 新随笔 联系 管理 订阅 <div class="blogStats"><!--done--> 随笔- 6  文章- 2  评论- 2 < ...

  6. Linux下Flash-LED的处理

    Linux下Flash-LED的处理 一些LED设备提供两种模式-torch和flash.在LED子系统中,LED类(参见Linux下的LED处理)和LED Flash类,分别支持这些模式.torch ...

  7. YOLOv4:目标检测(windows和Linux下Darknet 版本)实施

    YOLOv4:目标检测(windows和Linux下Darknet 版本)实施 YOLOv4 - Neural Networks for Object Detection (Windows and L ...

  8. Linux下的C#连接Mysql数据库

    今天在尝试在 Linux 系统下使用C#连接数据库,发现网上这方面的信息很少,所以就写一篇博客记录一下. Linux下这里使用的是mono. 首先是缺少Mysql.Data.dll这个库的,所以需要安 ...

  9. mysql在linux下的安装

    mysql在linux下的安装 安装环境:系统是 centos6.5 1.下载 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads ...

最新文章

  1. 谷歌 Java 编程风格指南
  2. pyspark使用ipython
  3. c语言如何用双重循环去重,c语言中一个一维数组怎样去重?
  4. php中符号 的作用是什么意思,PHP中@符号的用途是什么?
  5. 计算机图形学图形旋转_计算机图形学翻译
  6. iPhone上传文件到ftp服务器,将文件上传到iPhone上的FTP服务器
  7. 数据科学 IPython 笔记本 8.7 密度和等高线图
  8. 民科微服务电脑版下载_天翼云桌面通用版电脑版下载|天翼云桌面通用版PC客户端 V1.23.0 官方最新版 下载_当下软件园...
  9. shellcode编写技巧
  10. 数据结构笔记(三十四)二叉排序树的删除
  11. yaf index.php,yaf框架访问路由为什么始终访问INDEX下的INDEX控制器下的INDEX方法
  12. Faiss从入门到实战精通
  13. 笔试c语言实现栈,栈的实现与操作(C语言实现)
  14. 2019大数据分析软件介绍
  15. 软件工程与计算II-5-需求基础
  16. 家用计算机的使用说明,AWIND奇机家用无线投屏器使用说明
  17. 利用root权限随意对app进行保资料降级 99%成功
  18. 详解WAF与极风云WAF
  19. 海龟绘图小案例(内含源码)
  20. C#笔试题面试题锦集

热门文章

  1. cglib源码分析--转
  2. Linux 精准获取进程pid--转
  3. 五大因素推动中国AI崛起,生态报告概览中国AI产业 By 机器之心2017年7月17日 12:51 中国的人工智能将会在全世界扮演什么样的角色?最近,风险投资机构Vertex发表了一份生态研究报告
  4. 开源是互联网发展的核动力
  5. java filter 返回错误消息_利用java filter 实现业务异常拦截 跳转到错误信息提示页面...
  6. Oracle-index索引解读
  7. java编程里的values怎么使用,详解Java编程中super关键字的用法
  8. 计算机网络怎么删除,怎么删除网络协议
  9. 项目实战-linux下安装activeMQ
  10. 基于多线程队列简单实现mq