Skip to content

Commit 12c21f8

Browse files
committed
added main2.c
1 parent 0415a94 commit 12c21f8

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

glthreads/glthread.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "glthread.h"
22
#include <stdlib.h>
3+
#include <stdio.h>
34

45
/*Private function to add a new_node right after curr_node*/
56
static void

glthreads/main2.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include "glthread.h"
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
#include <memory.h>
5+
6+
typedef struct emp_ {
7+
8+
char name[30];
9+
unsigned int salary;
10+
char designation[30];
11+
unsigned int emp_id;
12+
glthread_node_t glnode;
13+
} emp_t;
14+
15+
void
16+
print_emp_details(emp_t *emp){
17+
18+
printf("Employee name = %s\n", emp->name);
19+
printf("salary = %u\n", emp->salary);
20+
printf("designation = %s\n", emp->designation);
21+
printf("emp_id = %u\n", emp->emp_id);
22+
}
23+
24+
25+
int
26+
main(int argc, char **argv){
27+
28+
emp_t *emp1 = calloc(1, sizeof(emp_t));
29+
strncpy(emp1->name, "Neha", strlen("Neha"));
30+
emp1->salary = 50000;
31+
strncpy(emp1->designation, "HR", strlen("HR"));
32+
emp1->emp_id = 21;
33+
glthread_node_init((&emp1->glnode));
34+
35+
emp_t *emp2 = calloc(1, sizeof(emp_t));
36+
strncpy(emp2->name, "Abhishek", strlen("Abhishek"));
37+
emp1->salary = 150000;
38+
strncpy(emp2->designation, "SE3", strlen("SE3"));
39+
emp2->emp_id = 32;
40+
glthread_node_init((&emp2->glnode));
41+
42+
emp_t *emp3 = calloc(1, sizeof(emp_t));
43+
strncpy(emp3->name, "Arun", strlen("Arun"));
44+
emp3->salary = 60000;
45+
strncpy(emp3->designation, "SE4", strlen("SE4"));
46+
emp3->emp_id = 27;
47+
glthread_node_init((&emp3->glnode));
48+
49+
50+
/*Now Create a glthread*/
51+
glthread_t *emp_list = calloc(1, sizeof(glthread_t));
52+
init_glthread(emp_list, offsetof(emp_t, glnode));
53+
54+
/*Now insert the records in glthread*/
55+
glthread_add(emp_list, &emp1->glnode);
56+
glthread_add(emp_list, &emp2->glnode);
57+
glthread_add(emp_list, &emp3->glnode);
58+
59+
60+
/*Walk over glthread*/
61+
emp_t *ptr = NULL;
62+
ITERATE_GL_THREADS_BEGIN(emp_list, emp_t, ptr){
63+
64+
print_emp_details(ptr);
65+
} ITERATE_GL_THREADS_ENDS;
66+
67+
/*Let us remove one record at random*/
68+
69+
glthread_remove(emp_list, &emp2->glnode);
70+
printf("\nprinting again . . . \n");
71+
72+
ITERATE_GL_THREADS_BEGIN(emp_list, emp_t, ptr){
73+
74+
print_emp_details(ptr);
75+
} ITERATE_GL_THREADS_ENDS;
76+
77+
78+
/*Free all Dynamicall allocations*/
79+
ITERATE_GL_THREADS_BEGIN(emp_list, emp_t, ptr){
80+
81+
glthread_remove(emp_list, &ptr->glnode);
82+
free(ptr);
83+
} ITERATE_GL_THREADS_ENDS;
84+
free(emp_list);
85+
86+
return 0;
87+
}

0 commit comments

Comments
 (0)