原文地址:http://blog.csdn.net/lzx_bupt/article/details/6910503
c/c++: 多线程编程基础讲解(一)
利用几篇文章简单记录下c与c++多线程编程基础入门的东西,每篇一个程序来记录,备忘。这些章节是由浅入深组织的,赖死狗!
在注释中讲吧,佛楼米!
- #include <iostream>
- #include <pthread.h>//头文件是必须的,符合posix标准使程序可移植众多平台
- using namespace std;
- #define NUM_THREADS 5
- void* say_hello(void* args)//线程的运行函数,必须void*,没说的表示返回通用指针、输入通用指针
- {
- cout << "hello..." << endl;
- }
- int main()
- {
- pthread_t tids[NUM_THREADS];//定义线程的id变量,多个变量可以声明为数组使用
- for(int i = 0; i < NUM_THREADS; ++i)
- {
- int ret = pthread_create(&tids[i], NULL, say_hello, NULL);//参数依次是:创建的线程id,线程参数,调用函数名,传入的函数参数
- if (ret != 0)
- {
- cout << "pthread_create error: error_code=" << ret << endl;
- }
- }
- pthread_exit(NULL);//等各个线程退出后,进程才结束,否则进程强制结束了,线程可能还没反应过来;
- }
编译命令:
g++ -lpthread -o test.out test.cpp
其中调用静态库文件pthread是必须的,然后运行测试,欧文!
- [cpp@node2 pthread]$ ./ex_create
- hello...
- hello...
- hello...
- hello...
- hello...
- [cpp@node2 pthread]$
c/c++: 多线程编程基础讲解(二)
在基础一上思考,如果线程调用的函数是在一个类中怎么办?答案是将该函数写成静态成员函数,如下模式就很符合C++的写作模式:
- #include <iostream>
- #include <pthread.h>
- using namespace std;
- #define NUM_THREADS 5
- class Hello
- {
- public:
- static void* say_hello(void* args)//除了多了static关键字,别无异样;
- {
- cout << "hello..." << endl;
- }
- };
- int main()
- {
- pthread_t tids[NUM_THREADS];
- for(int i = 0; i < NUM_THREADS; ++i)
- {
- int ret = pthread_create(&tids[i], NULL, Hello::say_hello, NULL);
- if (ret != 0)
- {
- cout << "pthread_create error: error_code=" << ret << endl;
- }
- }
- pthread_exit(NULL);
- }
c/c++: 多线程编程基础讲解(三)
线程会创建了,如何在线程调用函数时,传入参数呢?则应如下所示:
- #include <iostream>
- #include <pthread.h>
- using namespace std;
- #define NUM_THREADS 5
- void* say_hello(void* args)
- {
- int i = *((int*)args);//对传入的参数进行强制类型转换,由无类型指针变为整形数指针,然后再读取;
- cout << "hello in " << i << endl;
- }
- int main()
- {
- pthread_t tids[NUM_THREADS];
- cout << "hello in main..." << endl;
- for(int i = 0; i < NUM_THREADS; ++i)
- {
- int ret = pthread_create(&tids[i], NULL, say_hello, (void *)&i);//传入的时候必须强制转换为void* 类型,即无类型指针
- cout << "Current pthread id =" << tids[i] << endl;//这里学会使用tids数组打印创建的进程id信息;
- if (ret != 0)
- {
- cout << "pthread_create error: error_code=" << ret << endl;
- }
- }
- pthread_exit(NULL);
- }
编译、运行,结果如下:
- Current pthread id =139671233451792
- Current pthread id =139671222961936
- Current pthread id =139671212472080
- Current pthread id =139671201982224
- Current pthread id =139671191492368
- hello in 4196496
- hello in 4196496
- hello in 4196496
- hello in 4196496
- hello in 4196496
是否发现了问题?对,i的值没有输出预想的结果,这是因为多线程造成的,主进程在i还未赋值时,线程已经开始跑啦!~
那么下面代码是正确的:
- #include <iostream>
- #include <pthread.h>
- using namespace std;
- #define NUM_THREADS 5
- void* say_hello(void* args)
- {
- cout << "hello in thread " << *((int *)args) << endl;
- }
- int main()
- {
- pthread_t tids[NUM_THREADS];
- int indexes[NUM_THREADS];//用个数组来保存i的值,就不会变了
- for(int i = 0; i < NUM_THREADS; ++i)
- {
- indexes[i] = i;//先保存i的值,在调用线程就不会出现问题了
- int ret = pthread_create( &tids[i], NULL, say_hello, (void *)&(indexes[i]) );
- if (ret != 0)
- {
- cout << "pthread_create error: error_code=" << ret << endl;
- }
- }
- for (int i = 0; i < NUM_THREADS; ++i)
- pthread_join(tids[i], NULL);
- }
编译、运行:(源程序去掉了打印线程id的废话)
- [cpp@node2 pthread]$ ./ex_create_args_ok
- hello in thread 3
- hello in thread 4
- hello in thread 2
- hello in thread 1
- hello in thread 0
c/c++: 多线程编程基础讲解(四)
经过前面的几个例子,是不是还少个线程创建时属性参数没有提到,见下文示例:
- #include <iostream>
- #include <pthread.h>
- #include <iostream>
- #include <pthread.h>
- using namespace std;
- #define NUM_THREADS 5
- void* say_hello(void* args)
- {
- cout << "hello in thread " << *((int *)args) << endl;
- int status = 10 + *((int *)args);//将参数加10
- pthread_exit((void*)status);//由于线程创建时候提供了joinable参数,这里可以在退出时添加退出的信息:status供主程序提取该线程的结束信息;
- }
- int main()
- {
- pthread_t tids[NUM_THREADS];
- int indexes[NUM_THREADS];
- pthread_attr_t attr;//要想创建时加入参数,先声明
- pthread_attr_init(&attr);//再初始化
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);//声明、初始化后第三步就是设置你想要指定线程属性参数,这个参数表明这个线程是可以join连接的,join功能表示主程序可以等线程结束后再去做某事,实现了主程序和线程同步功能,这个深层理解必须通过图示才能解释;参阅其他资料吧
- for(int i = 0; i < NUM_THREADS; ++i)
- {
- indexes[i] = i;
- int ret = pthread_create( &tids[i], &attr, say_hello, (void *)&(indexes[i]) );//这里四个参数都齐全了,更多的配置仍需查阅资料;
- if (ret != 0)
- {
- cout << "pthread_create error: error_code=" << ret << endl;
- }
- }
- pthread_attr_destroy(&attr);//参数使用完了就可以销毁了,必须销毁哦,防止内存泄露;
- void *status;
- for (int i = 0; i < NUM_THREADS; ++i)
- {
- int ret = pthread_join(tids[i], &status);//前面创建了线程,这里主程序想要join每个线程后取得每个线程的退出信息status;
- if (ret != 0)
- {
- cout << "pthread_join error: error_code=" << ret << endl;
- }
- else
- {
- cout << "pthread_join get status: " << (long)status << endl;
- }
- }
- }
编译运行 g++ -lpthread -o ex_join ex_join.cpp
结果:
- hello in thread 4
- hello in thread 3
- hello in thread 2
- hello in thread 1
- hello in thread 0
- pthread_join get status: 10
- pthread_join get status: 11
- pthread_join get status: 12
- pthread_join get status: 13
- pthread_join get status: 14
体会一下join的功能吧
c/c++: 多线程编程基础讲解(五)
本篇进入难点了,mutex互斥锁概念,mutex=mutual exclusion的缩写,顺便说一句:以前老师都爱用缩写,也不跟同学说全称,这尼玛能理解深刻么!下文是用法:
- #include <iostream>
- #include <pthread.h>//按规矩不能少
- using namespace std;
- #define NUM_THREADS 5
- int sum = 0;//定义个全局变量,让所有线程进行访问,这样就会出现同时写的情况,势必会需要锁机制;
- pthread_mutex_t sum_mutex;
- void* say_hello(void* args)
- {
- cout << "hello in thread " << *((int *)args) << endl;
- pthread_mutex_lock (&sum_mutex);//修改sum就先加锁,锁被占用就阻塞,直到拿到锁再修改sum;
- cout << "before sum is " << sum << " in thread " << *((int *)args) << endl;
- sum += *((int *)args);
- cout << "after sum is " << sum << " in thread " << *((int *)args) << endl;
- pthread_mutex_unlock (&sum_mutex);//完事后解锁,释放给其他线程使用;
- pthread_exit(0);//退出随便扔个状态码
- }
- int main()
- {
- pthread_t tids[NUM_THREADS];
- int indexes[NUM_THREADS];
- //下三句是设置线程参数没啥可说的
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
- pthread_mutex_init (&sum_mutex, NULL);//这句是对锁进行初始化,必须的;
- for(int i = 0; i < NUM_THREADS; ++i)
- {
- indexes[i] = i;
- int ret = pthread_create( &tids[i], &attr, say_hello, (void *)&(indexes[i]) );//5个进程去你们去修改sum吧哈哈;
- if (ret != 0)
- {
- cout << "pthread_create error: error_code=" << ret << endl;
- }
- }
- pthread_attr_destroy(&attr);//删除参数变量
- void *status;
- for (int i = 0; i < NUM_THREADS; ++i)
- {
- int ret = pthread_join(tids[i], &status);
- if (ret != 0)
- {
- cout << "pthread_join error: error_code=" << ret << endl;
- }
- }
- cout << "finally sum is " << sum << endl;
- pthread_mutex_destroy(&sum_mutex);//注销锁,可以看出使用pthread内置变量神马的都对应了销毁函数,估计是内存泄露相关的吧;
- }
惯例:g++ -lpthread -o ex_mutex ex_mutex.cpp
运行:
- hello in thread 4
- before sum is 0 in thread 4
- after sum is 4 in thread 4
- hello in thread 3
- before sum is 4 in thread 3
- after sum is 7 in thread 3
- hello in thread 2
- before sum is 7 in thread 2
- after sum is 9 in thread 2
- hello in thread 1
- before sum is 9 in thread 1
- after sum is 10 in thread 1
- hello in thread 0
- before sum is 10 in thread 0
- after sum is 10 in thread 0
- finally sum is 10
发现个现象,thread4先运行,很诡异吧而i是从0递增的,所以呢多线程的顺序是混乱的,混乱就是正常;只要sum访问及修改是正常的,就达到多线程的目的了,运行顺序不能作为参照;
c/c++: 多线程编程基础讲解(六)
上篇说了下互斥量的用法,今儿说一下条件信号量的用法,这两种多线程变量的用法其实取决于情景,需要体会,见文:
- #include <iostream>
- #include <pthread.h>//带头文件
- #include <stdio.h>
- using namespace std;
- #define BOUNDARY 5
- int tasks = 10;
- pthread_mutex_t tasks_mutex;//因为两个线程要修改一个全局变量,需要互斥量;
- pthread_cond_t tasks_cond;//因为两个线程间有条件关系:当tasks>5时,hello2处理它,处理一次减少1;反之hello1处理,直到tasks减为零;
- void* say_hello2(void* args)//hello2处理函数
- {
- pthread_t pid = pthread_self();//打印当前线程id便于跟踪
- cout << "["<< pid << "] hello in thread " << *((int*)args) << endl;
- bool is_signaled = false;//随便一个标志位
- while(1)//无限循环
- {
- pthread_mutex_lock(&tasks_mutex);//要修改了,加锁
- if (tasks > BOUNDARY)//>5才修改
- {
- cout << "["<< pid << "] take task: "<< tasks << " in thread "<< *((int*)args) << endl;
- --tasks;//减少1
- }
- else if (!is_signaled)
- {
- cout << "["<< pid << "] pthread_cond_signal in thread " << *((int*)args) << endl;
- pthread_cond_signal(&tasks_cond);//表明已经不是>5了告诉hello1进程去处理:发送信号;
- is_signaled = true;//表示信号已经发送了
- }
- pthread_mutex_unlock(&tasks_mutex);//操作完解锁
- if (tasks == 0) break;//必须等待tasks全部减为零即hello1完成操作,才跳出循环结束这个进程
- }
- }
- <p>void* say_hello1(void* args)//<=5处理函数
- {
- pthread_t pid = pthread_self();
- cout << "["<< pid << "] hello in thread " << *((int*)args) << endl;</p><p> while(1)
- {
- pthread_mutex_lock(&tasks_mutex);
- if (tasks > BOUNDARY)//如果>5说明需要hello2处理,那么该线程就需要等待
- {
- cout << "["<< pid << "] pthread_cond_wait in thread " << *((int*)args) << endl;
- pthread_cond_wait(&tasks_cond, &tasks_mutex);//等待信号量生效,当hello2发出信号,这里就跳出wait,执行后续;
- }
- else
- {
- cout << "["<< pid << "] take task: "<< tasks << " in thread "<< *((int*)args) << endl;
- --tasks;//<=5就--
- }
- pthread_mutex_unlock(&tasks_mutex);</p><p> if (tasks == 0) break;//为零时退出,同hello2一样
- }</p><p>}</p><p>int main()
- {
- pthread_attr_t attr;//线程创建为joinable的,使得主进程可以和两个线程同步,两个线程完成工作退出后,主进程再退出;
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);</p><p> pthread_mutex_init(&tasks_mutex, NULL);//初始化互斥量
- pthread_cond_init(&tasks_cond, NULL);//初始化条件信号量</p><p> pthread_t tid1, tid2;//用于保存两个线程的id号
- int index1 = 1;
- int ret = pthread_create( &tid1, &attr, say_hello1, (void *)&index1);
- if (ret != 0)
- {
- cout << "pthread_create error: error_code=" << ret << endl;
- }</p><p> int index2 = 2;
- ret = pthread_create( &tid2, &attr, say_hello2, (void *)&index2);
- if (ret != 0)
- {
- cout << "pthread_create error: error_code=" << ret << endl;
- }</p><p> </p><p> pthread_join(tid1, NULL);//连接两个线程
- pthread_join(tid2, NULL);</p><p> pthread_attr_destroy(&attr);//该销毁的销毁
- pthread_mutex_destroy(&tasks_mutex);
- pthread_cond_destroy(&tasks_cond);</p><p> //正常退出
- }</p><p> </p>
惯例:g++ -lpthread -o ex_cond ex_cond.cpp
执行结果:
- [cpp@node2 pthread]$ ./ex_cond
- [140009886947088] hello in thread 2
- [140009886947088] take task: 10 in thread 2
- [140009886947088] take task: 9 in thread 2
- [140009886947088] take task: 8 in thread 2
- [140009886947088] take task: 7 in thread 2
- [140009886947088] take task: 6 in thread 2
- [140009886947088] pthread_cond_signal in thread 2
- [140009897436944] hello in thread 1
- [140009897436944] take task: 5 in thread 1
- [140009897436944] take task: 4 in thread 1
- [140009897436944] take task: 3 in thread 1
- [140009897436944] take task: 2 in thread 1
- [140009897436944] take task: 1 in thread 1
/*********************我是华丽的分割线**********************/
编后语
在第一小节了,作者就提到了“#include <pthread.h>//头文件是必须的,符合posix标准使程序可移植众多平台”。
POSIX是可移植的操作系统接口(Portable Operating System Interface of Unix) 的缩写。由IEEE开发,由ANSI和ISO标准化。目的在于提高应用程序在各种unix操作系统环境之间的可移植性。也就是:使得任何符合POSIX标准的应用程序都可以在重新编译后运行于任何符合posix标准的OS上。然而,POSIX 并不局限于 UNIX。Linux基本上逐步实现了POSIX兼容,但并没有参加正式的POSIX认证。微软的Windows NT至少部分实现了POSIX兼容。[链接]
我们暂时可以简单的理解为作者给出的示例程序是linux/unix平台下的代码,而无法在windows平台下运行调试。
所以在整个系列篇幅中,没有见到windows平台下创建线程的API CreateThread() 。