2013-03-19 102 views
0

我需要从线程返回整数值的帮助。我尝试过几件事情,但无法实现它的功能。我是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; 
} 
+0

OT:怎么样使用'else if'或'switch'。至少可以节省缩进和大括号。 – alk 2013-03-19 19:07:39

回答

1

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

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

1

首先是你要注意出头:

int i, num, *next_ptr; 
... 

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

您发送相同的地址(&num)所有线程,而分别读取数为每个线程。因为它们都有一个指向相同数字的指针,所以它们很可能都会取最后一个值(或者任意指定它们的任何赋值或更新值)。因此,作为论点,您需要拥有与线程数量一样多的num

要返回整数,您可以将整型转换为可以工作的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)->ret。当然,你仍然需要像线程一样多的ar

1

由于next在您的代码中,因此您不应该返回指向线程函数本地变量的指针。原因与其他函数几乎相同 - 函数退出时(线程终止时)变量的生存期结束,所以指向它的指针是不可靠的。相反,请尝试执行以下操作之一:

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

或者,在线程中动态分配int并返回一个指向它的指针;当你完成它的时候,你会从主线程中释放它。

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