zoukankan      html  css  js  c++  java
  • Linux pthread

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <pthread.h>
    #include <unistd.h>
    
    //#######################################################
    //
    //	李刚
    //	2016.8.17
    //	pthread 线程参数传递
    //
    //########################################################
    
    struct CTime{
    	int id;
    	char name[12];
    	char Num[12];
    };
    
    void *GetThread(void *arg){
    	struct CTime *t_time = (struct CTime *)arg;	
    //	printf("pthread_t=%d
    ",(int)pthread_self());
    	
    	printf("%d
    " , t_time->id);
    	printf("%s
    " , t_time->name);
    	printf("%s
    " , t_time->Num);
    
    	return 0;
    }
    
    void *GetInstace(void * arg){
    	int* a = ((void **)arg)[0];
    	float* b = ((void **)arg)[1];
    	char* c = ((void **)arg)[2];
    
    	printf("this is thread:  %d,  %.3f,  %s
    ", *a, *b, c);
    
    	return 0;
    }
    
    int main(int argc, char*argv[]){
    	pthread_t tid; 
    	int err = 0;
    	pthread_attr_t attr;	
    	struct CTime t_time;
    	int a = 12;
    	float b = 23.5f;
    	char c[] = "hello,world";
    
    	void *arg[3] = {&a, &b, c}; // 
    
    	t_time.id =12;
    	memcpy(t_time.name, "tom json", sizeof("tom json"));
    	memcpy(t_time.Num, "1234567", sizeof("1234567"));	
    
    
    	pthread_attr_init(&attr); //
    	if((err = pthread_create(&tid, &attr, &GetThread, (void *) &t_time)) != 0)
    		printf("Error err = %d
    ", err);
    
    	pthread_join(tid, NULL);
    		
    	if((err = pthread_create(&tid, &attr, &GetInstace, (void *) arg)) != 0)
    		printf("Error 
    ");
    
    	pthread_join(tid, NULL);
    		
    		
    		return 0;
    }
    

      

  • 相关阅读:
    华为花了100亿,为员工造了12个欧洲小镇,幸福到爆棚
    Qt5.9 官方发布的新版本亮点的确不胜枚举(而且修复2000+ bugs)
    详尽分析世纪之战:360VS腾讯是两个阶层的抗争
    c#
    PhantomJS
    bootstrap table
    Linux 入门
    多路搜索树
    网站性能优化工具
    NetCore上传多文件
  • 原文地址:https://www.cnblogs.com/vagabond/p/5800918.html
Copyright © 2011-2022 走看看