如何从各种线程返回值并在C末尾添加它们 [英] How do I return a value from various threads and add them at the end in C

查看:63
本文介绍了如何从各种线程返回值并在C末尾添加它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部好,



我正在尝试在C中运行一个简单的程序,我将文件映射到内存位置并使用线程搜索它。我也应用了互斥锁,因此线程不会改变我的输出。我的代码正在正确编译并正确执行,但是现在我似乎无法从线程函数返回一个int值。



我的输出示例如下:



函数中count的值是2598,当我使用
返回它时


HI all,

I am trying to run a simple program in C where I map a file to a memory location and search through it using threads. I have also applied the mutex lock, so threads don't change my output. My code is compiling properly and executing properly, however now I can't seem to return an int value from the thread function.

Example of my output would be as under:

The value of count in the function is 2598 and when I return it using

printf("\n%d is the count of the word",(int)status);





它给我的东西像-143003104或者711181824





以下是我的代码,请告诉我我是否遗漏了什么。





it gives me something like -143003104 or 711181824


Below is my code, please tell me if I am missing anything.

#define BUFFERSIZE 64
int count=0;
char * data;
struct stat filestat;
pthread_t mid[100];
pthread_mutex_t lock;

struct thread_data{
  int begin;
  int end;
  char *word;
};

struct thread_data thread_data_array[100];

/* Name : s_help()
 * parameters: None
 * Returns : void
 * Description: To give out the help statements
 */
void *s_workerSearch(void *threadarg)
{
  pthread_mutex_lock(&lock);
  printf("\ninside workersearch for a new thread");
  struct thread_data *my_data;
  my_data=(struct thread_data *) threadarg;
  int begin=my_data->begin;
  int end= my_data->end;
  char *word= my_data->word;
  printf("\nbeginning is %d\n",begin);
  printf("\n end is %d",end);
  printf("\n to search %s ",word);
  int len=strlen(word);
  printf("%d",len);
  int j;
  for(j=begin;j<end;j++)
   {
     printf("\n inside for not inside if\n");
    if(!memcmp(data+j,word,strlen(word)))
     {
      printf("inside if for workersearch");
      count++;
     }
   }
 printf("\n count of the word inside the workersearch is %d",count);
 pthread_mutex_unlock(&lock);
 return (void *)count;
}


void s_search(char **args)
{

  char *word=args[1];
  int worker=atoi(args[2]);
  printf("no of workers %d\n",worker);
  int size=0;
  size_t segment;
  pid_t pid;
  int i=0;
  int j=0;
  int count=0;
  char *w_ptr;
  int err;
  int status;

 for(i=0;i<worker;i++)
 {
  
   int begin = i / (float)worker * filestat.st_size;
   int end   = ( i + 1 )/ (float)worker * filestat.st_size;
  
   thread_data_array[i].begin=begin;
   thread_data_array[i].end=end;
   thread_data_array[i].word=word;
    err=pthread_create(&(mid[i]),NULL,s_workerSearch,(void *) &thread_data_array[i]);
   if (err!=0) printf("\ncant create thread");
   else printf("success!!!");

   }
printf("\n%d is the count of the word",(int)status);
}

                                                                                                                                                                   71,2          10%





我的尝试:



我试过使用状态返回count的值,但是,我的类型转换是否有问题?



另外,我是否加入主题并返回状态,如果是,我该怎么做呢?



此外,在编译时我收到一条警告说



警告:从不同大小的整数转换为指针[-Wint-to-pointer-施法器]

返回(void *)count;



What I have tried:

I have tried to return the value of count using the status, however, is there something wrong with my typecasting?

Also, do I join the threads and return the status, if yes, how do I go about doing that?

Also, while compiling I am getting an warning which says

warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
return (void *)count;

推荐答案

好吧,所以当我把相同的for循环时,我的代码得到修复(我的主要功能中的一个给出。但是,现在我似乎无法从线程函数返回计数。因此编辑我的问题。



okay, so my code got fixed when I put the same for loop(the one given below) in my main function. However, now I can't seem to return the count from the thread function. Hence editing my question.

for(j=begin;j<end;j++)>
   {
     printf("\n inside for not inside if\n");
    if(!memcmp(data+j,word,strlen(word)))
     {
      printf("inside if for workersearch");
      count++;
     }
   }


这篇关于如何从各种线程返回值并在C末尾添加它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆