• C++多线程的使用


    很多的时候会遇到多线程跑 ,接下来就写了一个 多线程的demo  废话不说直接上代码

    #include <iostream>
    #include <pthread.h> //多线程相关操作头文件,可移植众多平台
    
    using namespace std;
    #define NUM_THREADS 5 //线程数
    
    void* say_hello( void* args )
    {
        int i = *( (int*)args ); //对传入的参数进行强制类型转换,由无类型指针转变为整形指针,再用*读取其指向到内容
        i++;
        cout << "i" << i << endl;
        cout << "hello..." << endl;
    } //函数返回的是函数指针,便于后面作为参数
    
    void* say_hello_w( void* args )
    {
        int i = *( (int*)args ); //对传入的参数进行强制类型转换,由无类型指针转变为整形指针,再用*读取其指向到内容
        i+2;
        cout << "i" << i << endl;
        cout << "say_hello_w" << endl;
    } //函数返回的是函数指针,便于后面作为参数
    void* say_hello_wo( void* args )
    {
        int i = *( (int*)args ); //对传入的参数进行强制类型转换,由无类型指针转变为整形指针,再用*读取其指向到内容
        i+3;
        cout << "i" << i << endl;
        cout << "say_hello_wo" << endl;
    } //函数返回的是函数指针,便于后面作为参数
    void* say_hello_wor( void* args )
    {
        int i = *( (int*)args ); //对传入的参数进行强制类型转换,由无类型指针转变为整形指针,再用*读取其指向到内容
        i+4;
        cout << "i" << i << endl;
        cout << "say_hello_wor" << endl;
    } //函数返回的是函数指针,便于后面作为参数
    
    
    int main(int argc, char *argv[])
    {
        pthread_t tids[NUM_THREADS]; //线程id
    //    cout << "Hello World!" << endl;
    
          int i=0;
    //        int ret = pthread_create( &tids[i], NULL, say_hello, NULL ); //参数:创建的线程id,线程参数,线程运行函数的起始地址,运行函数的参数
            int ret = pthread_create( &tids[0], NULL, say_hello, (void*)&i ); //传入到参数必须强转为void*类型,即无类型指针,&i表示取i的地址,即指向i的指针
            if( ret != 0 ) //创建线程成功返回0
            {
                cout << "pthread_create error:error_code=" << ret << endl;
            }
    
             ret = pthread_create( &tids[1], NULL, say_hello_w, (void*)&i ); //传入到参数必须强转为void*类型,即无类型指针,&i表示取i的地址,即指向i的指针
            if( ret != 0 ) //创建线程成功返回0
            {
                cout << "pthread_create error:error_code=" << ret << endl;
            }
    
             ret = pthread_create( &tids[2], NULL, say_hello_wo, (void*)&i ); //传入到参数必须强转为void*类型,即无类型指针,&i表示取i的地址,即指向i的指针
            if( ret != 0 ) //创建线程成功返回0
            {
                cout << "pthread_create error:error_code=" << ret << endl;
            }
    
             ret = pthread_create( &tids[3], NULL, say_hello_wor, (void*)&i ); //传入到参数必须强转为void*类型,即无类型指针,&i表示取i的地址,即指向i的指针
            if( ret != 0 ) //创建线程成功返回0
            {
                cout << "pthread_create error:error_code=" << ret << endl;
            }
    
    
        pthread_exit( NULL ); //等待各个线程退出后,进程才结束,否则进程强制结束,线程处于未终止的状态
    
        return 0;
    }
    

     在多线程个过程中如果对全局变量进行修改的时候要用互斥锁。

    #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 ); //是设置你想要指定线程属性参数,这个参数表明这个线程是可以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 ); //注销锁  
    }  
    

    代码在  http://pan.baidu.com/s/1jIrEXP8

  • 相关阅读:
    JSP实现页面自动跳转
    marquee属性的使用说明
    jsp 按钮 超链接 直接跳转至另一页面
    生成验证码图片代码
    js 通过判断月数填充日数下拉框
    邮箱正则验证
    jsp 验证用正则表达式
    onselectstart 与 -moz-user-select
    onselectstart 、onselect区别
    NSOperation基本操作
  • 原文地址:https://www.cnblogs.com/wanghuixi/p/7561610.html
Copyright © 2020-2023  润新知