title: Thread - safe hash table written in C
body: <p>I am trying to create a custom thread safe hash table in C using <code>std::atomic</code>. Here is my code:</p>
  
  <p>ts_hash_table.hpp:</p>
  
  <pre><code>#ifndef TS_HASH_TABLE_H
  #define TS_HASH_TABLE_H
  
  #include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;atomic&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #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&lt;ELEMENT*&gt;* 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
  </code></pre>
  
  <p>ts_hash_table.cpp:</p>
  
  <pre><code>#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-&gt;buckets = (std::atomic&lt;ELEMENT*&gt;*)malloc(sizeof(ELEMENT*)*size);
  
      if (hash_table-&gt;buckets == NULL)
          return NULL;
  
      for (int i = 0; i &lt; size; i++)
          hash_table-&gt;buckets[i] = NULL;
  
      hash_table-&gt;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-&gt;next = hash_table -&gt; buckets[hashIndex].load();
     while(!hash_table -&gt; buckets[hashIndex].compare_exchange_weak(new_element-&gt;next, new_element));
  
  }   
  
  
  ELEMENT* get(int key) {
  
     int hashIndex = calculateHash(key);  
  
     ELEMENT* element = hash_table-&gt;buckets[hashIndex];
     while(element != NULL) {
  
        if(element-&gt;key == key)
           return element; 
  
        element = element -&gt; next;
  
        hashIndex %= TABLE_SIZE;
     }        
  
     return NULL;        
  }
  
  ELEMENT* createElement(int key, int data){
  
     ELEMENT* element = (ELEMENT*) malloc(sizeof(ELEMENT));
  
     element-&gt;data = data;  
     element-&gt;key = key;
     element-&gt;next = NULL;
  
     return element;
  }
  
  int calculateHash(int key) {
     return key % TABLE_SIZE;
  }
  
  void printTable() {
  
     for(int i = 0; i &lt; TABLE_SIZE; i++) {
  
        printf("[BUCKET %d]: ", i);
  
        if(hash_table -&gt; buckets[i].load() != NULL)
           printList(hash_table -&gt;buckets[i].load());
        else
           printf("EMPTY");
  
        printf("\n");
     }
  
  }
  
  void printList(ELEMENT* head){
  
      while (head != NULL){
          printf(" (%d, %d)", head-&gt;key, head-&gt;data);
          head = head-&gt;next;
      }
  
  }
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>#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;
  }
  </code></pre>
  
  <p>Inside <strong><em>ts_hash_table.cpp</em></strong> you can see that I have tried to implement insertion into the hash table using <code>compare_excahnge_weak</code> function. For some reason, I am getting <strong>segmentation fault</strong>. What is not clear to me is that even when I try to print something at start of the <code>insert</code> function, I cannot see it. Not just that, but when I compile and run the program (using <code>g++ *.cpp -o main</code>), the print I have put in main function doesn show even thoguh I am calling <code>printf</code> before <code>insert</code>. But when I comment out calls to insert everything works fine. </p>
  
  <p>Any suggestions? Am I using atomic function wrong way?</p>
  
  <p>EDIT:</p>
  
  <p>I used <code>gdb</code> in order to debug the code and it seems like the segfault happens when I try to use <code>load()</code> function</p>
  
  <p><a href="https://i.stack.imgur.com/TtF0n.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TtF0n.png" alt="enter image description here"></a></p>