コード例 #1
0
ファイル: p_thread2.c プロジェクト: Tarintote/GoLang
int main(int ac, char **av){

  struct timeval s,t;
  double startTime, endTime;

  if(ac != 3){
    printf("引数はスレッドの数、分割する分の数だけを指定して下さい\n");
    exit(1);
  }
  
  ret_p rets;
  if((rets = struct_init(sizeof(int*), atoi(av[1]), atoi(av[2]))) == NULL)
    return -1;
  int Number = rets->max;
  int N = rets->N;
  pthread_t th[Number];  
  pthread_mutex_init(&mutex,NULL);//--------pthread初期化

  int status;
  long double SUM = 0.0L;
  void *sum[Number];
  void *save;
  
  srand((unsigned)time(NULL));  
  gettimeofday(&s, NULL);//----------------時間計測開始

  int *x;
  x = malloc(sizeof(int));
  for(int i=0; i<Number; i++){
    x = &i;
    if(struct_push(rets, x) == -1){
      return -1;
    }
  }
  
  save = rets;
  for(int i=0; i<Number; i++){
    //-------pthread作成
    status = pthread_create(&th[i],NULL,(void *)Thread_Func,(void *)save);

    if(status != 0){
      perror("can not create thread ");
      exit(EXIT_FAILUER);
    }    
  }
  
  for(int i=0; i<Number; i++){
    if(pthread_join(th[i], &sum[i]) != 0){//--------pthread待ち状態
      perror("can not join thread");
      exit(EXIT_FAILUER);
    }
  }
  
  pthread_mutex_destroy(&mutex);//--------------pthreadデストローイ
  
  for(int i=0; i<Number; i++)
    SUM += *(int *)sum[i];

  free(*sum);
  SUM = (SUM/N)*4.0L;
  
  gettimeofday(&t, NULL);//--------------------時間計測終了
  startTime = s.tv_sec + (double)(s.tv_usec * 1e-6);
  endTime = t.tv_sec + (double)(t.tv_usec * 1e-6);
  
  //printf("%f ms\t PI = %.30Lf\n",(endTime - startTime) * 1000, SUM);
  printf("%d %f %.30Lf\n", N/1000000, (endTime - startTime) * 1000, SUM);
  struct_free(rets);
  return 0;
}
コード例 #2
0
ファイル: json_message.c プロジェクト: biparadox/cube-1.2
int json_2_message(char * json_str,void ** message)
{

    void * root_node;
    void * head_node;
    void * tag_node;
    void * record_node;
    void * curr_record;
    void * expand_node;
    void * curr_expand;

    void * record_value;
    void * expand_value;

    struct message_box * msg_box;
    MSG_HEAD * msg_head;
    MSG_EXPAND * msg_expand;
    int record_no;
    int expand_no;
    void * precord;
    void * pexpand;
    int i;
    int ret;
    char buffer[DIGEST_SIZE];

    int offset;
    int type;
    int subtype;

    offset=json_solve_str(&root_node,json_str);
    if(offset<0)
        return offset;

    // get json node's head
    head_node=json_find_elem("HEAD",root_node);
    if(head_node==NULL)
    	head_node=json_find_elem("head",root_node);
    if(head_node==NULL)
        return -EINVAL;
    tag_node=json_find_elem("tag",head_node);
    if(tag_node!=NULL)   // default tag value is "MESG"
    {
    	ret=json_node_getvalue(tag_node,buffer,10);
    	if(ret!=4)
        	return -EINVAL;
	if(Memcmp(buffer,"MESG",ret)!=0)
		return -EINVAL;
    }	
    msg_box=message_init();
    msg_head=message_get_head(msg_box);
    json_2_struct(head_node,msg_head,msg_box->head_template);


    // get json node's record
    // init message box
    ret=message_record_init(msg_box);
    if(ret<0)
        return ret;

    record_node=json_find_elem("RECORD",root_node);
    if(record_node==NULL)
    	record_node=json_find_elem("record",root_node);
    if(record_node==NULL)
        return -EINVAL;

    curr_record=json_get_first_child(record_node);
    if(curr_record==NULL)
         return -EINVAL;
    char node_name[DIGEST_SIZE*2];
    ret=json_node_getname(curr_record,node_name);
    if(!strcmp(node_name,"BIN_FORMAT"))
    {
	BYTE * radix64_string;
	radix64_string=malloc(4096);
	if(radix64_string==NULL)
		return -ENOMEM;
	ret=json_node_getvalue(curr_record,radix64_string,4096);
	if(ret<0)
		return -EINVAL;
	int radix64_len=strnlen(radix64_string,4096);
	msg_head->record_size=radix_to_bin_len(radix64_len);
	msg_box->blob=malloc(msg_head->record_size);
	if(msg_box->blob==NULL)
		return -ENOMEM;
	ret=radix64_to_bin(msg_box->blob,radix64_len,radix64_string);
   }
    else
   {
    	for(i=0;i<msg_head->record_num;i++)
    	{
        	if(curr_record==NULL)
            		return -EINVAL;
        	ret=Galloc0(&precord,struct_size(msg_box->record_template));
        	if(ret<=0)
            		return -EINVAL;
       		json_2_struct(curr_record,precord,msg_box->record_template);
        	message_add_record(msg_box,precord);
        	curr_record=json_get_next_child(record_node);
	}
    }

    // get json_node's expand
    expand_no=msg_head->expand_num;
    msg_head->expand_num=0;
    expand_node=json_find_elem("EXPAND",root_node); 
    if(expand_node==NULL)
    	expand_node=json_find_elem("expand",root_node);
    if(expand_node!=NULL)
    {
	char buf[20];
	void * curr_expand_template;
	curr_expand=json_get_first_child(expand_node);
   	for(i=0;i<expand_no;i++)
    	{
        	if(curr_expand==NULL)
            		return -EINVAL;
		ret=Galloc0(&msg_expand,struct_size(message_get_expand_template()));
		if(ret<0)
			return -ENOMEM;

		ret=json_2_struct(curr_expand,&msg_expand,message_get_expand_template());
		if(ret<0)
			return ret;

		void * tempnode;
		if((tempnode=json_find_elem(curr_expand,"BIN_DATA"))==NULL)
		{

				
			curr_expand_template=memdb_get_template(msg_expand->type,msg_expand->subtype);
			if(curr_expand_template==NULL)
				return -EINVAL;
			struct_free(msg_expand,message_get_expand_template());			
			ret=Galloc(&msg_expand,struct_size(curr_expand_template));
			if(ret<0)
				return -ENOMEM;
			ret=json_2_struct(curr_expand,msg_expand,curr_expand_template);
			if(ret<0)
				return ret;
		}
		

	
        	message_add_expand(msg_box,msg_expand);
        	curr_expand=json_get_next_child(expand_node);

	}
    }


    *message=msg_box;
    msg_box->box_state = MSG_BOX_RECOVER;
    return offset;
}