B
B
Blunker2015-02-23 13:40:17
linux
Blunker, 2015-02-23 13:40:17

POSIX THREAD what's the problem?

int main()
{
    struct rb_tree *tree;
    int i, rc;
    unsigned int t;
    pthread_t a[2];
    
    srand(time(NULL));
    
    t = clock();
    
    for (i = 0; i < 2; ++i)
    {
        rc = pthread_create(&a[i], NULL, do_operation, tree);
        printf("%d\n", rc);
    }
    
    for (i = 0; i < 2; ++i)
    {
        pthread_join(a[i], NULL);
    }
  
    
    t = clock() - t;
    
    printf("time: %d sec.\n", t);
    
    Print(tree, 0);
    
    return 0;
}

void *do_operation(void *arg)
{
    struct rb_tree *tree = arg;
    int i = 0;
    
    while (i != 100)
    {
        tree = Insert(tree, rand()%100);
        i++;
    }
    
    pthread_exit(NULL);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bogolt, 2015-02-23
@bogolt

It looks like your tree variable is not initialized. More precisely, no memory is allocated for it, because it's just a pointer for you.
Well, and so - use a debugger, arrange prints at different points in the program to understand where it has reached and where it has not, remove the excess to further narrow the area of ​​the error. And you will be happy to find answers on your own.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question