Linux 多线程

1、下载 http://zthread.sourceforge.net

2、编译 ZTHREADS 2.3.1 - Ubuntu 18.04.2 LTS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

ZTHREADS 2.3.1 - Ubuntu 18.04.2 LTS


HOWTO COMPILE THE SOURCE:

change to main directory like : ZThread-2.3.2

before install need to check :
libtoolize , m4 , automake


step :

1 ./configure


2 libtoolize --copy --force

aclocal
3 aclocal -I share/


4 autoconf


5 automake


6 automake --add-missing


7 ./configure --prefix=/home/alang/temp/ CXXFLAGS="$CXXFLAGS -fpermissive"


8 make


9 make install


Note: install to /usr/local/ need root

3、测试

1
2
3
4
5
6
7
8
g++ test1.cpp -o main -lzthread
/usr/bin/ld: 找不到 -lzthread
collect2: error: ld returned 1 exit status


g++ test1.cpp -o main -I /usr/local/include/ -L /usr/local/lib/ -lZThread
./main
./main: error while loading shared libraries: libZThread-2.3.so.2: cannot open shared object file: No such file or directory

error while loading shared libraries: XXXXXX: cannot open shared object file: No such file or directory

解决方案一:
在/etc/ld.so.conf中加入libcurl.so所在的目录。一般so档案会在/usr/local/lib这个目录下(用户可以自定义目录),所以在/etc/ld.so.conf中加入/usr/local/lib(或用户自定义的目录)这一行,

最后,保存/etc/ld.so.conf,执行命令[root@www]# /sbin/ldconfig -v才能生效。

解决方案二:

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
所以安装共享库后要注意共享库路径设置问题, 如下:
1) 如果共享库文件安装到了/lib或/usr/lib目录下, 那么需执行一下ldconfig命令
ldconfig命令的用途, 主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下, 搜索出可共享的动态链接库(格式如lib*.so*), 进而创建出动态装入程序(ld.so)所需的连接和缓存文件. 缓存文件默认为/etc/ld.so.cache, 此文件保存已排好序的动态链接库名字列表.

2) 如果共享库文件安装到了/usr/local/lib(很多开源的共享库都会安装到该目录下)或其它"非/lib或/usr/lib"目录下, 那么在执行ldconfig命令前, 还要把新共享库目录加入到共享库配置文件/etc/ld.so.conf中, 如下:
cat /etc/ld.so.conf
include ld.so.conf.d/*.conf

如上所示:/etc/ld.so.conf配置文件中内容只有一行,
ld.so.conf.d/*.conf的意思就是包含ld.so.conf.d/目录下以.conf为后缀的文件
所以我们可以在/etc/ld.so.conf.d目录下新建一个libevent.conf的配置文件,然后把libevent安装路径配置好
我的libevent内容如下:
# cat libevent.conf
# /usr/libevent/lib

配置完后执行以下ldconfig命令
# ldconfig

3) 如果共享库文件安装到了其它"非/lib或/usr/lib" 目录下, 但是又不想在/etc/ld.so.conf中加路径(或者是没有权限加路径). 那可以export一个全局变量LD_LIBRARY_PATH, 然后运行程序的时候就会去这个目录中找共享库.
LD_LIBRARY_PATH的意思是告诉loader在哪些目录中可以找到共享库. 可以设置多个搜索目录, 这些目录之间用冒号分隔开. 比如安装了一个mysql到/usr/local/mysql目录下, 其中有一大堆库文件在/usr/local/mysql/lib下面, 则可以在.bashrc或.bash_profile或shell里加入以下语句即可:

export LD_LIBRARY_PATH=/usr/local/mysql/lib:$LD_LIBRARY_PATH
一般来讲这只是一种临时的解决方案, 在没有权限或临时需要的时候使用.

4) 此方案跟3处理类似,只不过在 ld.so.conf 中加入相应的库路径,
在/etc/ld.so.conf中加入/usr/local/lib这一行,保存之后,
再运行:ldconfig 或者 /sbin/ldconfig –v 更新一下配置即可。

4、实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*

g++ test1.cpp -o main -I /usr/local/include/ -L /usr/local/lib/ -lZThread

*/
#include <iostream>
#include <zthread/Runnable.h>
#include <zthread/Thread.h>

using namespace std;
using namespace ZThread; // Zthread所有的变量,类等都在这个名字空间内

//这个跟java的风格一样
class Counter : public Runnable
{
private:
int _id; // 给任务一个识别id
int _num; // 计数器
public:
Counter(int id):_id(id){
_num = 0; //构造函数初始化
}
void run() // 实现run函数
{
_num = 1;
while(_num <= 50)
{
cout <<"我的id :"<<_id << ": " << _num << endl;
_num++;
Thread::sleep(500 * (_id+1));
}
}
};
int main()
{
//创建线程
Thread t0(new Counter(0));
Thread t1(new Counter(1));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*

g++ test2.cpp -o main -I /usr/local/include/ -L /usr/local/lib/ -lZThread

gcc test2.cpp -o main -I /usr/local/include/ -L /usr/local/lib/ -lZThread -lstdc++

*/
#include <iostream>
#include "zthread/Runnable.h"
#include "zthread/Thread.h"
using namespace std;

using namespace ZThread; // Zthread所有的变量,类等都在这个名字空间内

class LiftOff : public ZThread :: Runnable
{
int countdown;
int id;
public:
LiftOff(int count,int ident=0):countdown(count),id(ident)
{
cout << "constructor " << endl;
}

~LiftOff()
{
cout << id << " completed " << endl;
}

void run()
{
while(countdown--)
{
cout << " run-->> "<< id << ":" << countdown << endl;
}
cout << id << " lift off " << endl;
}

};

int main()
{
try
{
Thread t(new LiftOff(10),true);

cout << "Waiting for LiftOff " << endl;
}
catch(Synchronization_Exception& e)
{
cerr << "Synchronization_Exception: " << e.what() << endl;
}
}

文档