zoukankan      html  css  js  c++  java
  • 多线程

    #include<stdlib.h>
    #include<pthread.h>
    #include<stdio.h>
    #include<unistd.h>
    
    struct member
    {
       int a;
       char* s;
    };
    /*线程1,不传递参数 */
    void* create1(void* arg)
    {
        printf("thread create1 
    ");
        return (void*)0;
    }
    /*线程2,传递一个int型参数*/
    void* create2(void* arg)
    {
        int* name;
        name=(int*)arg;
        printf("thread create2 %d
    ",*name);
        return 0;
    }
    /*线程3,传递一个string型参数*/
    void* create3(void* arg)
    {
        char* str;
        str=(char*)arg;
        printf("thread create3 %s 
    ",str);
        return 0;
    }
    
    /*线程4,传递一个struct结构体*/
    void* create4(void* arg)
    {
        struct member *sm;
        sm=(struct member *)arg;
        printf("thread create4 a=%d,s=%s 
    ",sm->a,sm->s);
        return 0;
    }
    
    int main(int argc, char** argv)
    {
        pthread_t pid1,pid2,pid3,pid4;
        int ret;
        int test=8;
        int* name=&test;
    
        char* a="hello world!";
    
        struct member *sm;
        sm=(struct member *)malloc(sizeof(struct member));
        sm->a=4;
        sm->s="struct test!";
    
        ret=pthread_create(&pid1,NULL,create1,NULL);
    
        pthread_join(pid1,NULL);
        if(!ret)
            printf("pthread_creat1 ok
    ");
    
        ret=pthread_create(&pid2,NULL,create2,(void*)name);
        pthread_join(pid2,NULL);
        if(!ret)
            printf("pthread_creat2 ok
    ");
    
        ret=pthread_create(&pid3,NULL,create3,(void*)a);
        pthread_join(pid3,NULL);
        if(!ret)
            printf("pthread_creat3 ok
    ");
    
        ret=pthread_create(&pid4,NULL,create4,(void*)sm);
        pthread_join(pid4,NULL);
        if(!ret)
            printf("pthread_creat4 ok
    ");
        return 0;
    }


  • 相关阅读:
    UVALive 7141 BombX
    CodeForces 722D Generating Sets
    CodeForces 722C Destroying Array
    CodeForces 721D Maxim and Array
    CodeForces 721C Journey
    CodeForces 415D Mashmokh and ACM
    CodeForces 718C Sasha and Array
    CodeForces 635C XOR Equation
    CodeForces 631D Messenger
    田忌赛马问题
  • 原文地址:https://www.cnblogs.com/bzyzhang/p/5399638.html
Copyright © 2011-2022 走看看