Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdlib.h>
- #include "list.c"
- typedef int my_type;
- typedef char my_type2;
- struct args{
- struct hash_table *arr;
- int *liczby;
- };
- int hash(void* element){
- my_type* tmp;
- tmp = (my_type *)element;
- return (*tmp) % 19;
- }
- void getF(void* element){
- my_type* tmp;
- tmp = (my_type*) element;
- printf("%d ", *(tmp));
- }
- int hash_ch(void* element){
- my_type2* tmp;
- tmp = (my_type2 *)element;
- return (*tmp) % 19;
- }
- void getF_ch(void* element){
- my_type2* tmp;
- tmp = (my_type2*) element;
- printf("%d ", *(tmp));
- }
- void* f_watku(void *arg){
- struct args* a = (struct args *)arg;
- int *liczby = a->liczby;
- int i;
- for(i=0; i<10; i++){
- liczby[i]= i;
- add(a->arr, &liczby[i]);
- }
- }
- void* f_watku2(void *arg){
- struct args* a = (struct args *)arg;
- int *liczby = a->liczby;
- int i;
- for(i=1; i<10; i++){
- delete(a->arr, &liczby[i]);
- }
- }
- void* f_watku3(void *arg){
- struct args* a = (struct args *)arg;
- int *liczby = a->liczby;
- int i;
- for(i=1; i<10; i++){
- delete(a->arr, &liczby[i]);
- }
- }
- int main(){
- struct hash_table* arr = create_table(hash, getF);
- pthread_t w1;
- pthread_t w2;
- pthread_t w3;
- pthread_t w4, w5, w6;
- struct args a1, a2, a3, a4;
- a1.arr = a2.arr = a3.arr = a4.arr = arr;
- a1.liczby = (int *)malloc(sizeof(int) * 10);
- a2.liczby = (int *)malloc(sizeof(int) * 10);
- a3.liczby = (int *)malloc(sizeof(int) * 10);
- pthread_create(&w1, NULL, f_watku, &a1);
- pthread_create(&w2, NULL, f_watku, &a2);
- pthread_create(&w5, NULL, f_watku, &a3);
- pthread_join(w1, NULL);
- pthread_join(w2, NULL);
- pthread_join(w5, NULL);
- print_table(arr);
- //pthread_create(&w3, NULL, f_watku2, &a1);
- pthread_create(&w4, NULL, f_watku2, &a2);
- pthread_create(&w6, NULL, f_watku2, &a3);
- //pthread_join(w3, NULL);
- pthread_join(w4, NULL);
- pthread_join(w6, NULL);
- print_table(arr);
- /*
- int a = 20;
- int b = 19;
- int c = 38;
- int *a = (int *)malloc(sizeof(int));
- *a = 5;
- int *b = (int *)malloc(sizeof(int));
- *b = 25;
- int *c = (int *)malloc(sizeof(int));
- *c = 15;
- int *d = (int *)malloc(sizeof(int));
- *d = 35;
- add(arr, a);
- add(arr, b);
- add(arr, c);
- add(arr, d);
- print_table(arr);
- delete(arr, b);
- print_table(arr);
- delete(arr, c);
- print_table(arr);
- delete(arr, d);
- print_table(arr);
- delete(arr, a);
- print_table(arr);*/
- return 0;
- }
- #include <stdio.h>
- #include <malloc.h>
- #include <pthread.h>
- struct list_elem {
- void* number;
- struct list_elem *next;
- };
- struct arr_elem {
- struct arr_elem *next;
- struct list_elem *cont;
- int key;
- };
- struct hash_table {
- pthread_mutex_t p_m;
- struct arr_elem *first;
- int(*hash)(void*);
- void(*getF)(void*);
- };
- void add(struct hash_table* arr, void* element)
- {
- pthread_mutex_lock(&arr->p_m);
- struct arr_elem *tmp = arr->first;
- while (tmp->next != NULL && tmp->next->key != arr->hash(element)) {
- tmp = tmp->next;
- }
- if (tmp->next == NULL) {
- tmp->next = (struct arr_elem *)malloc(sizeof(struct arr_elem));
- tmp->next->key = arr->hash(element);
- tmp->next->next = NULL;
- tmp->next->cont = (struct list_elem *)malloc(sizeof(struct list_elem));
- tmp->next->cont->number = element;
- tmp->next->cont->next = NULL;
- }
- else {
- struct list_elem *tmpl = tmp->next->cont;
- while (tmpl->next != NULL && tmpl->next->number != element) {
- tmpl = tmpl->next;
- }
- if (tmpl->next == NULL) {
- tmpl->next = (struct list_elem *)malloc(sizeof(struct list_elem));
- tmpl->next->number = element;
- tmpl->next->next = NULL;
- }
- }
- pthread_mutex_unlock(&arr->p_m);
- }
- void delete(struct hash_table* arr, void* element){
- pthread_mutex_lock(&arr->p_m);
- struct arr_elem *tmp = arr->first;
- while (tmp != NULL && tmp->key != arr->hash(element)) {
- tmp = tmp->next;
- }
- if(tmp == NULL){
- pthread_mutex_unlock(&arr->p_m);
- return;
- }
- else{
- struct list_elem *tmpl = tmp->cont, *tmpl2;
- if(tmpl->next == NULL){
- free(tmpl);
- tmp->cont = NULL;
- //tmp23 = tmp->next;
- //free(tmp->next);
- //tmp->next = tmp23;
- pthread_mutex_unlock(&arr->p_m);
- return;
- }
- while (tmpl->next != NULL && tmpl->next->number != element) {
- tmpl = tmpl->next;
- }
- if(tmpl->next != NULL){
- tmpl2 = tmpl->next->next;
- free(tmpl->next);
- tmpl->next = tmpl2;
- }
- }
- pthread_mutex_unlock(&arr->p_m);
- }
- void clear_table(struct hash_table* arr) {
- struct arr_elem *tmp = arr->first->next;
- struct arr_elem *tmp2;
- struct list_elem *contTmp, *contTmp2;
- while (tmp != NULL) {
- tmp2 = tmp->next;
- contTmp = tmp->cont;
- while (contTmp != NULL) {
- contTmp2 = contTmp->next;
- free(contTmp);
- contTmp = contTmp2;
- }
- free(tmp);
- tmp = tmp2;
- }
- free(arr->first);
- }
- void get(struct hash_table* arr, int key) {
- struct arr_elem *tmp = arr->first;
- while (tmp != NULL && tmp->key != key) {
- tmp = tmp->next;
- }
- if (tmp == NULL) {
- printf("no data with key %d\n", key);
- }
- else {
- struct list_elem *tmpl = tmp->cont;
- printf("key %d :", key);
- while (tmpl != NULL) {
- arr->getF(tmpl->number);
- tmpl = tmpl->next;
- }
- printf("\n");
- }
- }
- void print_table(struct hash_table* arr) {
- struct arr_elem *tmp = arr->first->next;
- while (tmp != NULL) {
- printf("key: %d, values: ", tmp->key);
- struct list_elem *tmpl = tmp->cont;
- while (tmpl != NULL) {
- arr->getF(tmpl->number);
- tmpl = tmpl->next;
- }
- printf("\n");
- tmp = tmp->next;
- }
- }
- struct hash_table* create_table(int(*hashF)(void*),void(*getF)(void*)) {
- struct hash_table* arr = (struct hash_table*)malloc(sizeof(struct hash_table));
- //arr->p_m = PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_init(&arr->p_m, NULL);
- arr->hash = hashF;
- arr->getF = getF;
- arr->first = (struct arr_elem *)malloc(sizeof(struct arr_elem));
- arr->first->key = -1;
- arr->first->next = NULL;
- arr->first->cont = NULL;
- return arr;
- }
- gcc -c -Wall -fPIC -D_GNU_SOURCE list.c
- gcc list.o -shared -o lib_list.so
- gcc -c main.c -o list_main.o
- gcc list_main.o -L. -pthread -l_list -o test_listdyn.out
- export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:."
- ./test_listdyn.out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement