2013-12-09 28 views
2

我有一个麻烦传递一个结构指针到一个函数,因为我有点与这些指针和引用混淆。我想修改thread_start函数的thread.thread_num值。线程结构作为函数参数C

#include <stdio.h> 
#include <stdlib.h> //malloc, free 
#include <pthread.h> 

#define N 5 

// void *malloc(size_t); 

struct thread { 
    pthread_t thread_id;  
    int  thread_num; 
    // int  thread_sum;  
}; 

void *thread_start(void *thread) 
{ 
    struct thread *my_data; 
    my_data = (struct thread *)thread; 
    printf("num T: %i\n", my_data->thread_num); 
    my_data->thread_num=4; 
    printf("num T: %i\n", my_data->thread_num); 

    return NULL; 
} 

int main(int argc, char *argv[]) 
{ 
    int i; 
    struct thread pthread_data; 
    struct thread *thread = &pthread_data; 

    thread->thread_num=2; 
    pthread_create(&thread->thread_id, NULL, thread_start, (void *)&thread); 
    printf("num: %i\n",thread->thread_num); 

    pthread_exit(NULL); 
    return 0; 
} 

但是打印主体的值不变(2)。

然后我想创建线程结构数组,但我不知道究竟是如何做到这一点: 我想应该是这样的:

int main(int argc, char *argv[]) 
{ 
    int i; 
    struct thread pthread_data; 
    struct thread *thread[N-1] = &pthread_data; // I don't know how to manage this. 
    for(i=0; i<N; i++) 
    { 
    thread->thread_num=i; 
    pthread_create(&thread[i]->thread_id, NULL, thread_start, (void *)&thread[i]); 
    printf("num %i: %i\n",i,thread[i]->thread_num); 
    } 
    pthread_exit(NULL); 
    return 0; 
} 

有什么想法?

+0

线程是异步的。 –

+0

你应该看看thread.join()。顺便说一句,该列表似乎缺少一个'}'作为'main'中'for'循环的一部分。 – KeithSmith

+0

struct thread pthread_data [5]; – qwr

回答

5

我建议你阅读http://www.advancedlinuxprogramming.com/alp-folder/alp-ch04-threads.pdf

在这里你想要的东西:

#define N 5 
typedef struct thread { 
    pthread_t thread_id;  
    int  thread_num; 
    // int  thread_sum;  
} ThreadData; 
void *thread_start(void *thread) 
{ 
    ThreadData *my_data = (ThreadData*)thread; 
    //there is no guarantee that prints will be in order 
    // we will use its initial thread->num ,cause it differs for each thread 
    //plus you will see how threads will behave 
    int order=my_data->thread_num; 
    printf("%i) before num T: %i\n",order, my_data->thread_num); 
    my_data->thread_num=4; 
    printf("%i) after assignment num T: %i\n",order ,my_data->thread_num); 

    return NULL; 
} 

int main(int argc, char *argv[]) 
{ 
    int i; 
    ThreadData thread[N]; 
    for(i=0; i<N; i++) 
    { 
     thread[i].thread_num=i; 
    pthread_create(&(thread[i].thread_id), NULL, thread_start, (void *)(thread+i)); 

    } 
    //wait for all threads 
    for (i = 0; i < N; i++) 
     pthread_join(thread[i].thread_id, NULL); 
    //print results of each thread 
    for (i = 0; i < N; i++) 
     printf(" %i)thread: number %i\n",i,thread[i].thread_num); 
    return 0; 
} 
0

你不能。一旦main退出,它在您拨打pthread_exit时会执行此操作,因此不再存在,因为它在main函数的本地。所以它不能被修改。

+0

这不是这个,而是一个简单的竞争条件,'main'在线程修改之前打印。 – hyde

+0

@hyde他有一个竞争条件,但这是他的问题的答案。 –

+0

“可打印主体的值不变”部分肯定发生在线程中的指针成为悬挂之前。 – hyde

1

我可以看到你的代码的多个错误。

首先你有你的指针错误。在第一个例子中,通过&pthread_datapthread_create就足够了,&threadthread指针的地址,所以你传递struct thread **到你的函数而不是struct thread *。在第二个例子中,您应该使用(void *) thread[i]&thread[i]又是struct thread **

当你希望每个线程都写入自己的线程数据时,你应该创建一个线程数据数组,以便每个线程都有自己的一块,否则你会遇到竞争状态。

正如其他人已经指出的,您应该在致电printf之前调用pthread_join以确保主线程将等待所有工作线程。

另请注意,如果您从其他函数调用产生这些线程的函数pthread_join,则必须确保胎面数据数组不会超出范围(在这种情况下,最好使用它malloc或全局数组)。