参考
https://www.cnblogs.com/njczy2010/p/5795083.html
njczy2010
博客园 首页 新随笔 联系 订阅 订阅 管理
随笔- 287 评论- 44 文章- 1
转 c++多线程编程
c++多线程编程
一直对多线程编程这一块很陌生,决定花一点时间整理一下。
os:ubuntu 10.04 c++
1.最基础,进程同时创建5个线程,各自调用同一个函数
1 #include <iostream>
2 #include <pthread.h> //多线程相关操作头文件,可移植众多平台
3
4 using namespace std;
5
6 #define NUM_THREADS 5 //线程数
7
8 void* say_hello( void* args )
9 {
10 cout << "hello..." << endl;
11 } //函数返回的是函数指针,便于后面作为参数
12
13 int main()
14 {
15 pthread_t tids[NUM_THREADS]; //线程id
16 for( int i = 0; i < NUM_THREADS; ++i )
17 {
18 int ret = pthread_create( &tids[i], NULL, say_hello, NULL ); //参数:创建的线程id,线程参数,线程运行函数的起始地址,运行函数的参数
19 if( ret != 0 ) //创建线程成功返回0
20 {
21 cout << "pthread_create error:error_code=" << ret << endl;
22 }
23 }
24 pthread_exit( NULL ); //等待各个线程退出后,进程才结束,否则进程强制结束,线程处于未终止的状态
25 }
输入命令:g++ -o muti_thread_test_1 muti_thread_test_1.cpp -lpthread
注意:
1)此为c++程序,故用g++来编译生成可执行文件,并且要调用处理多线程操作相关的静态链接库文件pthread。
2)-lpthread 编译选项到位置可任意,如g++ -lpthread -o muti_thread_test_1 muti_thread_test_1.cpp
3)注意gcc和g++的区别,转到此文:点击打开链接
测试结果:
1 wq@wq-desktop:~/coding/muti_thread$ ./muti_thread_test_1
2 hello...hello...
3 hello...
4 hello...
5
6 hello...
1 wq@wq-desktop:~/coding/muti_thread$ ./muti_thread_test_1
2 hello...hello...hello...
3
4 hello...
5 hello...
可知,两次运行的结果会有差别,这不是多线程的特点吧?这显然没有同步?还有待进一步探索…
多线程的运行是混乱的,混乱就是正常?
2.线程调用到函数在一个类中,那必须将该函数声明为静态函数函数
因为静态成员函数属于静态全局区,线程可以共享这个区域,故可以各自调用。
1 #include <iostream>
2 #include <pthread.h>
3
4 using namespace std;
5
6 #define NUM_THREADS 5
7
8 class Hello
9 {
10 public:
11 static void* say_hello( void* args )
12 {
13 cout << "hello..." << endl;
14 }
15 };
16
17 int main()
18 {
19 pthread_t tids[NUM_THREADS];
20 for( int i = 0; i < NUM_THREADS; ++i )
21 {
22 int ret = pthread_create( &tids[i], NULL, Hello::say_hello, NULL );
23 if( ret != 0 )
24 {
25 cout << "pthread_create error:error_code" << ret << endl;
26 }
27 }
28 pthread_exit( NULL );
29 }
测试结果:
1 wq@wq-desktop:~/coding/muti_thread$ ./muti_thread_test_2
2 hello...
3 hello...
4 hello...
5 hello...
6 hello...
1 wq@wq-desktop:~/coding/muti_thread$ ./muti_thread_test_2
2 hello...hello...hello...
3
4
5 hello...
6 hello...
3.如何在线程调用函数时传入参数呢?
先看下面修改的代码,传入线程编号作为参数:
1 #include <iostream>
2 #include <pthread.h> //多线程相关操作头文件,可移植众多平台
3
4 using namespace std;
5
6 #define NUM_THREADS 5 //线程数
7
8 void* say_hello( void* args )
9 {
10 int i = *( (int*)args ); //对传入的参数进行强制类型转换,由无类型指针转变为整形指针,再用*读取其指向到内容
11 cout << "hello in " << i << endl;
12 } //函数返回的是函数指针,便于后面作为参数
13
14 int main()
15 {
16 pthread_t tids[NUM_THREADS]; //线程id
17 cout << "hello in main.." << endl;
18 for( int i = 0; i < NUM_THREADS; ++i )
19 {
20 int ret = pthread_create( &tids[i], NULL, say_hello, (void*)&i ); //传入到参数必须强转为void*类型,即无类型指针,&i表示取i的地址,即指向i的指针
21 cout << "Current pthread id = " << tids[i] << endl; //用tids数组打印创建的进程id信息
22 if( ret != 0 ) //创建线程成功返回0
23 {
24 cout << "pthread_create error:error_code=" << ret << endl;
25 }
26 }
27 pthread_exit( NULL ); //等待各个线程退出后,进程才结束,否则进程强制结束,线程处于未终止的状态
28 }
测试结果:
1 wq@wq-desktop:~/coding/muti_thread$ ./muti_thread_test_3
2 hello in main..
3 Current pthread id = 3078458224
4 Current pthread id = 3070065520
5 hello in hello in 2
6 1
7 Current pthread id = hello in 2
8 3061672816
9 Current pthread id = 3053280112
10 hello in 4
11 Current pthread id = hello in 4
12 3044887408
显然不是想要的结果,调用顺序很乱,这是为什么呢?
这是因为多线程到缘故,主进程还没开始对i赋值,线程已经开始跑了…?
修改代码如下:
1 #include <iostream&