@Blunker

Как решить проблему с Pthread?

Имеется многопоточная программа на языке С, когда пытаюсь создать rwmutex-ы комплилятор ругается, на то что тип pthread_rwlock_t найден. С pthreap_mutex_t все работает хорошо. В чем может быть проблема?
p.s. Компилятор gcc 4.9.2

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <math.h>

#define COLOR_RED 0
#define COLOR_BLACK 1
#define _ERROR_SUCCESS_ 1
#define THREADS 20

pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;

const int iter = pow(10, 6);
static int in = 0;
static int de = 0;

struct rb_tree
{
    int key;
    int color;
    int n;
    
    struct rb_tree *parent;
    struct rb_tree *left;
    struct rb_tree *right;
};


struct rb_tree EmptyNode = {0, COLOR_BLACK, 0, 0, 0};
struct rb_tree *NullNode = &EmptyNode;

struct rb_tree *Insert(struct rb_tree *root, int key);
struct rb_tree *fixup_insert(struct rb_tree *root, struct rb_tree *node);
struct rb_tree *left_rotate(struct rb_tree *root, struct rb_tree *node);
struct rb_tree *right_rotate(struct rb_tree *root, struct rb_tree *node);
void Print(struct rb_tree *tree, int lvl);
struct rb_tree *lookup(struct rb_tree *root, int key);
struct rb_tree *min(struct rb_tree *root);
struct rb_tree *max(struct rb_tree *root);
struct rb_tree *transplant(struct rb_tree *root, struct rb_tree *node1, struct rb_tree *node2);
struct rb_tree *fixup_delete(struct rb_tree *root, struct rb_tree *node);
struct rb_tree *delet(struct rb_tree *root, int key);
void *do_operation(void *arg);

int getrand(int min, int max)
{
    return (double)rand() / (RAND_MAX + 1.0) * (max - min) + min;
}

double wtime()
{
    struct timeval t;
    
    gettimeofday(&t, NULL);
    
    return (double)t.tv_sec + (double)t.tv_usec * 1E-6;
}


int main()
{
    struct rb_tree *tree =  NullNode;
    int i, rc, join;
    double t;
    pthread_t a[THREADS];
    for (i = 0; i < 100000; i++)
        tree = Insert(tree, rand() % 100);
    
    for (i = 0; i < THREADS; i++)
    {
        if ((rc = pthread_create(&a[i], NULL, do_operation, tree)) != 0)
        {
            printf("Error creating thread\n");
            exit(_ERROR_SUCCESS_);
        }
    }
    
    for (i = 0; i < THREADS; i++)
    {
        if ((join = pthread_join(a[i], (void**)&tree)) != 0)
        {
            printf("Error joined thread\n");
            exit(_ERROR_SUCCESS_);
        }
    }
    
    Print(tree, 0);
    
    return 0;
}

struct rb_tree *Insert(struct rb_tree *root,
        int key)
{
    struct rb_tree *node, *parent  = NullNode;
    
    pthread_rwlock_rdlock(&lock);
    
    for (node = root; node != NullNode && node != NULL;)
    {
        parent = node;

        if (key < node->key)
            node = node->left;
        else if (key > node->key)
            node = node->right;
        else
        {
            
            pthread_rwlock_unlock(&lock);
            
            return root;
        }
    }

    node = malloc(sizeof(*node));

    node->key = key;
    node->color = COLOR_RED;
    node->parent = parent;
    node->left = NullNode;
    node->right = NullNode;

    if (parent != NullNode)
    {
        if (key < parent->key)
            parent->left = node;
        else
            parent->right = node;
    }
    else
    {
        root = node;
    }

    return fixup_insert(root, node);
}


Вот кусок кода.
main.c:13:1: error: unknown type name ‘pthread_rwlock_t’
 pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;
 ^
main.c:13:25: error: ‘PTHREAD_RWLOCK_INITIALIZER’ undeclared here (not in a function)
 pthread_rwlock_t lock = PTHREAD_RWLOCK_INITIALIZER;


и вот ошибки
  • Вопрос задан
  • 494 просмотра
Пригласить эксперта
Ответы на вопрос 2
jcmvbkbc
@jcmvbkbc
"I'm here to consult you" © Dogbert
#include <pthread.h>

int main()
{
        pthread_rwlock_t lock;

        return 0;
}

Нормально компилируется. Покажите ваш код и сообщение об ошибке.
Ответ написан
@Rainberd
Вы случайно не с -std=c99 компилируете? Там pthread_rwlock_t недоступна, попробуйте с -std=gnu99
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы