码迷,mamicode.com
首页 > 系统相关 > 详细

Linux pthread

时间:2016-08-23 22:05:10      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

#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\n",(int)pthread_self());
	
	printf("%d\n" , t_time->id);
	printf("%s\n" , t_time->name);
	printf("%s\n" , 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\n", *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\n", err);

	pthread_join(tid, NULL);
		
	if((err = pthread_create(&tid, &attr, &GetInstace, (void *) arg)) != 0)
		printf("Error \n");

	pthread_join(tid, NULL);
		
		
		return 0;
}

  

Linux pthread

标签:

原文地址:http://www.cnblogs.com/vagabond/p/5800918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!