日期:2014-05-16  浏览次数:20884 次

ZThread库在Linux下安装

版权声明 :转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://linuxmem.blogbus.com/logs/47311596.html

ZThread 是 一个优秀的开放源码的C++线程库,Bruce Eckel在Thinking in C++第二卷的第11章讲述C++并发编程的时候即以此为基础。ZThread库在使用的时候与Java多线程编程很相似,对于有Java多线程编程经验 的程序员来说也非常容易上手:)

下载地址:http://zthread.sourceforge.net/download.html

当前ZThread版本为2.3.2,下载之后解压,按照configure-make-make install的“三部曲”编译安装,出现问题。

问题提示:

g++ -DHAVE_CONFIG_H -I. -I. -I. -I../include -g -O2 -Wall -DNDEBUG -g -O2 -Wall -DNDEBUG -MT AtomicCount.lo -MD -MP -MF .deps/AtomicCount.Tpo -c AtomicCount.cxx??-fPIC -DPIC -o .libs/AtomicCount.o
In file included from vanilla/SimpleAtomicCount.cxx:26,
? ?? ?? ?? ?? ???from AtomicCount.cxx:55:
../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
../include/zthread/Guard.h:494: error: (if you use `-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)

以下解决办法转载自:http://bbs.chinaunix.net/viewthread.php?tid=928981 ,感谢coldwarm网友!

=============================================================

这个是编译器编译器版本的问题.印象里Zthread的作者已经停止开发一段时间了.

g++3.4以后版本的名字查找方法和以前的版本不同,问题这里有解释.http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html .

实际上解决方法编译结果已经说了.
一种是使用-fpermissive开关来允许老的语法,
用export CXXFLAGS=-fpermissive,然后在configure

另一种方法可以在调用函数时加this.
this->isDisabled()

还可以把isDisabled()在那个类头部分重新声明一下.
using BaseClass<T>::isDisabled();

=============================================================

我使用第一种方法安装成功之后,修改已经安装好的头文件zthread/Guard.h:(注意,是已经拷贝到安装目录的那个,同时也是以后写程序 要include的那个,而不是源代码里面那个)第494行, 就是上面提到的第2步, 如果不做, 以后调用Guard.h还是会出错的。

搞定之后,写个程序测试一下:)

=============================================================

? 1 #include <iostream>
? 2 #include <string>
? 3 #include <zthread/Thread.h>
? 4 #include <zthread/Runnable.h>
? 5
? 6 using namespace std;
? 7 using namespace ZThread;
? 8
? 9 class Counter : public Runnable {
?10???????? int _count;
?11???????? int _id;
?12???? public:
?13???????? Counter(int count, int id = 0) :
?14???????????? _count(count), _id(id) {}
?15???????? ~Counter() {
?16???????????? cout << "Counter " << _id << " completed!" << endl;
?17???????? }
?18???????? void run() {
?19???????????? try {
?20???????????????? while (_count--) {
?21???????????????????? cout << "Counter " << _id << ": " << _count << endl;
?22???????????????????? Thread::sleep(10);
?23???????????????? }
?24???????????????? cout << "Counter " << _id << ": end" << endl;
?25???????????? } catch (Interrupted_Exception& e) {
?26???????????????? cerr << e.what() << endl;
?27???????????? }
?28???????? }
?29 };
?30
?31 int main() {
?32???? const int n = 5;
?33???? try {
?34???????? for (int i = 0; i < n; i++) {
?35?????????? Thread t(new Counter(10, i));
?36???????? }
?37???? } catch (Synchronization_Exception& e) {
?38???????? cerr << e.what() << endl;
?39???? }
?40 }

=============================================================

编译应该没问题,运行的时候可能会有错误: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? cp 到/usr/lib目录应该就可以运行了。

运行结果如下:

=============================================================

Counter 0: 9
Counter 1: 9
Counter 2: 9
Counter 1: 8
Counter 0: 8
Counter 3: 9
Counter 4: 9
Counter 0: 7<