c++多线程编程( 二 )

测试结果:
wq@wq-desktop:~/coding/muti_thread$ ./muti_thread_test_3hello in thread hello in thread hello in thread hello in thread hello in thread 30124这是正常的吗?感觉还是有问题...待续
代码中如果没有pthread_join主线程会很快结束从而使整个进程结束,从而使创建的线程没有机会开始执行就结束了 。加入pthread_join后,主线程会一直等待直到等待的线程结束自己才结束,使创建的线程有机会执行 。
4.线程创建时属性参数的设置pthread_attr_t及join功能的使用
线程的属性由结构体pthread_attr_t进行管理 。
typedef struct{ int detachstate; 线程的分离状态 int schedpolicy; 线程调度策略 struct sched_param schedparam; 线程的调度参数 int inheritsched; 线程的继承性int scope; 线程的作用域size_t guardsize; 线程栈末尾的警戒缓冲区大小int stackaddr_set; void * stackaddr; 线程栈的位置size_t stacksize; 线程栈的大小}pthread_attr_t;#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); //线程退出时添加退出的信息,status供主程序提取该线程的结束信息 pthread_exit( ( void* )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; } }}测试结果:
wq@wq-desktop:~/coding/muti_thread$ ./muti_thread_test_4hello in thread hello in thread hello in thread hello in thread 0hello in thread 3214pthread_join get status:10pthread_join get status:11pthread_join get status:12pthread_join get status:13pthread_join get status:145.互斥锁的实现
互斥锁是实现线程同步的一种机制,只要在临界区前后对资源加锁就能阻塞其他进程的访问 。
#include <iostream>#include <pthread.h>using namespace std;#define NUM_THREADS 5int 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 ); //是设置你想要指定线程属性参数,这个参数表明这个线程是可以join连接的,join功能表示主程序可以等线程结束后再去做某事,实现了主程序和线程同步功能 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 ); //主程序join每个线程后取得每个线程的退出信息status if( ret != 0 ) {cout << "pthread_join error:error_code=" << ret << endl; } } cout << "finally sum is " << sum << endl; pthread_mutex_destroy( &sum_mutex ); //注销锁}


推荐阅读