在循环内动态分配struct

时间:2015-05-20 10:10:08

标签: c multithreading struct

我写了一个简单的程序来玩pthread s。

每个线程作为线程数据接收指向struct的指针并打印它。然后我编写了一个循环来动态分配struct,然后使用线程创建中的点。

问题是,为什么所有线程都收到相同的stuct,尽管我已经分配了不同的循环?

#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS     5

struct thread_data {
    long thread_id;
    int thread_sum;
};

void *PrintHello(void *threadData)
{

    struct thread_data  *data_input = (struct thread_data *)threadData;

    printf("Hello World! It's me, thread #%ld! ,%d , pointer %d\n", data_input->thread_id, data_input->thread_sum, data_input);
    pthread_exit(NULL);
}


int main(int argc, char *argv[])
{
    pthread_t threads[NUM_THREADS];;
    struct thread_data allData[NUM_THREADS];
    int rc;
    long t;
    for (t = 0; t < NUM_THREADS; t++){

        struct thread_data* data = malloc(sizeof(struct thread_data));

        //allData[t].thread_id = t;
        //allData[t].thread_sum = 100;

        //  struct thread_data data;
        data->thread_id = t;
        data->thread_sum = 0;

        printf("In main: creating thread %ld  , %d , %d\n", data->thread_id, data->thread_sum, &data);
        rc = pthread_create(&threads[t], NULL, PrintHello, (void *)&data);
        if (rc){
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }
    }

    /* Last thing that main() should do */
    pthread_exit(NULL);

    return 0;
}

2 个答案:

答案 0 :(得分:2)

您的代码public class LocationRepository : ILocationRepository { private DefaultConnection dbContext; private DbSet<Location> DbSet; public LocationRepository() { dbContext = new DefaultConnection(); DbSet = dbContext.Set<Location>(); } public async Task InsertAsync(Location entity) { DbSet.Add(entity); await dbContext.SaveChangesAsync(); } #region IDisposable public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing) { if (dbContext != null) { dbContext.Dispose(); } } } #endregion } 上的

属于&data类型 替换这个

struct thread_data **

由此

 rc = pthread_create(&threads[t], NULL, PrintHello, (void *)&data);

另外,你必须为 rc = pthread_create(&threads[t], NULL, PrintHello, (void *)data); 函数<{1}}内的每个已分配内存调用free {/ 1}}函数

答案 1 :(得分:0)

直接使用数据

#include <pthread.h>                                                           
#include <stdio.h>                                                             
#include <stdlib.h>                                                            

#define NUM_THREADS     5                                                      

struct thread_data {                                                           
    long thread_id;                                                            
    int thread_sum;                                                            
};                                                                             

void *PrintHello(void *threadData)                                             
{                                                                              

    struct thread_data  *data_input = (struct thread_data *)threadData;        

    printf("Hello World! It's me, thread #%ld! ,%d , pointer %d\n", data_input->thread_id, data_input->thread_sum, data_input); 
    pthread_exit(NULL);                                                        
}                                                                              


int main(int argc, char *argv[])                                               
{                                                                              
    pthread_t threads[NUM_THREADS];;                                           
    struct thread_data allData[NUM_THREADS];                                   
    int rc;                                                                    
    long t;                                                                    
    for (t = 0; t < NUM_THREADS; t++){                                         

        struct thread_data* data = malloc(sizeof(struct thread_data));         

        //allData[t].thread_id = t;                                            
        //allData[t].thread_sum = 100;                                         

        //  struct thread_data data;                                           
        data->thread_id = t;                                                   
        data->thread_sum = 0;                                                  

        printf("In main: creating thread %ld  , %d , %d\n", data->thread_id, data->thread_sum, &data); 
        rc = pthread_create(&threads[t], NULL, PrintHello, (void *)data);      
        if (rc){                                                               
            printf("ERROR; return code from pthread_create() is %d\n", rc);    
            exit(-1);                                                          
        }                                                                      
    }                                                                          
    /* Last thing that main() should do */                                 
    pthread_exit(NULL);                                                    

    return 0;                                                              
} 

将输出:

In main: creating thread 0  , 0 , -273609272
In main: creating thread 1  , 0 , -273609272
Hello World! It's me, thread #0! ,0 , pointer 13357072
In main: creating thread 2  , 0 , -273609272
In main: creating thread 3  , 0 , -273609272
In main: creating thread 4  , 0 , -273609272
Hello World! It's me, thread #2! ,0 , pointer 13357712
Hello World! It's me, thread #1! ,0 , pointer 13357392
Hello World! It's me, thread #4! ,0 , pointer 13359632
Hello World! It's me, thread #3! ,0 , pointer 13358032