线程-用C语言编写的安全哈希表

时间:2019-03-20 08:58:33

标签: c++ multithreading concurrency atomic

我正在尝试使用std::atomic在C中创建自定义线程安全哈希表。这是我的代码:

ts_hash_table.hpp:

#ifndef TS_HASH_TABLE_H
#define TS_HASH_TABLE_H

#include <iostream>
#include <thread>
#include <atomic>
#include <stdio.h>
#include <stdlib.h>

#define TABLE_SIZE 10
#define THREAD_NUM 10

typedef struct element{
       int data;
       int key;
       struct element* next;
}ELEMENT;

typedef struct {
    int size;
    std::atomic<ELEMENT*>* buckets;
}HASH_TABLE;

extern HASH_TABLE* hash_table;

HASH_TABLE* createHashTable(int);
ELEMENT* createElement(int,int);
ELEMENT* get(int);

int calculateHash(int);
void insert(int, int);
void printList(ELEMENT*);
void printTable();  


#endif // TS_HASH_TABLE_H

ts_hash_table.cpp:

#include "ts_hash_table.hpp"

HASH_TABLE* createHashTable(int size){

    HASH_TABLE* hash_table = (HASH_TABLE*)malloc(sizeof(HASH_TABLE));

    if (hash_table == NULL)
        return NULL;

    hash_table->buckets = (std::atomic<ELEMENT*>*)malloc(sizeof(ELEMENT*)*size);

    if (hash_table->buckets == NULL)
        return NULL;

    for (int i = 0; i < size; i++)
        hash_table->buckets[i] = NULL;

    hash_table->size = size;

    return hash_table;
}

void insert(int key,int data) {
   printf("INSERTING INTO HASH TABLE");

   ELEMENT* new_element = NULL;  

   int hashIndex = calculateHash(key);

   new_element->next = hash_table -> buckets[hashIndex].load();
   while(!hash_table -> buckets[hashIndex].compare_exchange_weak(new_element->next, new_element));

}   


ELEMENT* get(int key) {

   int hashIndex = calculateHash(key);  

   ELEMENT* element = hash_table->buckets[hashIndex];
   while(element != NULL) {

      if(element->key == key)
         return element; 

      element = element -> next;

      hashIndex %= TABLE_SIZE;
   }        

   return NULL;        
}

ELEMENT* createElement(int key, int data){

   ELEMENT* element = (ELEMENT*) malloc(sizeof(ELEMENT));

   element->data = data;  
   element->key = key;
   element->next = NULL;

   return element;
}

int calculateHash(int key) {
   return key % TABLE_SIZE;
}

void printTable() {

   for(int i = 0; i < TABLE_SIZE; i++) {

      printf("[BUCKET %d]: ", i);

      if(hash_table -> buckets[i].load() != NULL)
         printList(hash_table ->buckets[i].load());
      else
         printf("EMPTY");

      printf("\n");
   }

}

void printList(ELEMENT* head){

    while (head != NULL){
        printf(" (%d, %d)", head->key, head->data);
        head = head->next;
    }

}

main.cpp:

#include "ts_hash_table.hpp"

HASH_TABLE* hash_table = createHashTable(TABLE_SIZE);

int main() {

   /*FILL IN THE TABLE*/

   printf("MAIN");
   insert(2,43);

   /*DISPLAY TABLE CONTENT*/

   printTable();

   return 0;
}

ts_hash_table.cpp 中,您可以看到我尝试使用compare_excahnge_weak函数来实现对哈希表的插入。由于某种原因,我遇到了分段错误。对我来说不清楚的是,即使我尝试在insert函数开始时打印某些内容,也看不到它。不仅如此,当我编译并运行程序(使用g++ *.cpp -o main)时,我在main函数中添加的打印图甚至不显示我在printf之前调用insert的情况。但是当我注释掉插入所有内容的调用时,一切正常。

有什么建议吗?我使用原子函数的方式错误吗?

编辑:

我使用gdb来调试代码,并且当我尝试使用load()函数时似乎发生了段错误

enter image description here

0 个答案:

没有答案