2013-03-19 75 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一樣大,但您不應該認爲它會這樣做)。