【问题标题】:Returning single integer value from thread从线程返回单个整数值
【发布时间】:2013-03-19 16:25:32
【问题描述】:

我需要帮助从线程返回整数值。我已经尝试了几件事,但无法让它工作。我是 C 新手,是的,这是家庭作业,但我被困住了,需要一些帮助。我已经在线程中创建了指针,但是当我将它传递回 main 时,它没有显示正确的值。我已经尝试过 malloc ,但这也不起作用。任何提示将不胜感激。

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<pthread.h>
#include "FindNextHigher.h"
#include "SortNumber.h"

void *thread_next(void *arg)
{
   /* Call FindNextHigher */
   int *num =(int *)arg;
   int next = FindNextHigher(*num);
   printf("next= %d\n", next);
   /* Convert int to pointer to pass and print to check*/
   int *next_ptr=&next;
   printf("nextptr= %d\n", *next_ptr);
   return (void *)next_ptr;
}

int main(int argc, char *argv[])
{
   FILE* f = fopen(argv[1], "r");
   int i, num, *next_ptr;
   printf("next_ptr = %d\n", *next_ptr);
   pthread_t thread1, thread2, thread3, thread4;
   void *exit_status;

   for(i=1; i<=20; i++)
   {
   fscanf(f, "%d", &num);
   if (i==1 || i<=60){
     pthread_create(&thread1, NULL, thread_next, &num);
     pthread_join(thread1, &exit_status);
     printf("Threadid 1 processes number %d which is %d and the next higher number is %d\n", i, num, *next_ptr);
     if (i==60){
        printf("--------------Process finished for Thread 1----------");
        } 
     }
   else {
   if (i==61 || i<=120){ 
     pthread_create(&thread2, NULL, thread_next, &num);
     pthread_join(thread2, &exit_status);
     printf("Threadid 2 processes number %d which is %d and the next higher number is %d\n", i, num, *next_ptr);
     if (i==120){
        printf("--------------Process finished for Thread 2----------");
     }
   }
   else{
   if (i==121 || i<=180){
     pthread_create(&thread3, NULL, thread_next, &num);
     pthread_join(thread3, &exit_status);
     printf("Threadid 3 processes number %d which is %d and the next higher number is %d\n", i, num, *next_ptr);
     if (i==180){
        printf("--------------Process finished for Thread 3----------");
      }
   }
   else{
   if (i==181 || i<=240){
     pthread_create(&thread4, NULL, thread_next, &num);
     pthread_join(thread4, &exit_status);
     printf("Threadid 4 processes number %d which is %d and the next higher number is %d\n", i, num, *next_ptr);
     if (i==240){
        printf("--------------Process finished for Thread 4----------");
     }
   }
   }
   }
   }
   }    
   return 0;
}

【问题讨论】:

  • OT:使用else ifswitch 怎么样。这样做至少可以节省缩进和大括号。

标签: c pthreads return pthread-join


【解决方案1】:

有一些错误。首先,在打印之前不要初始化next_ptr

接下来,您在 4 个线程之间共享指向同一个变量实例的指针,而没有任何同步机制来保护对它的访问。您将需要使用临界区或互斥体/信号量。

【讨论】:

    【解决方案2】:

    首先,您需要注意以下几点:

    int i, num, *next_ptr;
    ...
    
    for(i=1; i<=20; i++)
    {
      fscanf(f, "%d", &num);
      ...
      pthread_create(&thread1, NULL, thread_next, &num);
    

    您正在向所有线程发送相同的地址 (&amp;num),同时分别读取每个线程的编号。由于它们都有一个指向相同数字的指针,因此很可能它们都将采用最后一个值(或任意分配的任何值或以后的值)。因此,作为参数,您需要拥有与线程一样多的 nums。

    要返回整数,您可以将整数强制转换为void *,这会起作用,但它并不完全是标准的。一种解决方案是通过参数为返回值提供一个位置:

    struct arg_and_return
    {
        int num;
        int ret;
    };
    
    struct arg_and_return ar;
    fscanf(f, "%d", &ar.num);
    pthread_create(&thread, NULL, thread_func, &ar);
    

    在线程内,填写((struct arg_and_return *)arg)-&gt;ret。当然,您仍然需要与线程一样多的ars。

    【讨论】:

      【解决方案3】:

      您不应返回指向线程函数本地变量的指针,因为next 在您的代码中。原因与任何其他函数几乎相同——当函数退出时(当线程终止时),变量的生命周期已经结束,因此指向它的指针是不可靠的。相反,请尝试以下方法之一:

      使用来自main的变量,并在启动时将其地址传递给您的线程函数,然后从线程中修改它(但不要在线程仍在运行时从线程外部触摸它,除非您使用互斥体)。

      或者,在线程中动态分配int,并返回一个指向它的指针;完成后,您可以将其从主线程中释放出来。

      或者,如果您确定表示是兼容的,您可以将 int 强制转换为指针类型并将其返回,然后在主线程中强制转换回 int。这是一种常见的方法,但您需要确保它可以在您的系统上运行(如果指针至少与 ints 一样大,它通常会起作用,但您不应该想当然地认为它会这样做)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-06
        • 2012-02-27
        相关资源
        最近更新 更多