12 void *consumer(__attribute__((unused)) void *arg)
16 while ((data = queue_deq(&queue, NULL)) != NULL) {
17 // In a real world scenario, this would be something CPU intensive
20 for (int i = 0; i < CHUNK_SIZE; i++)
23 // How to convert char to int with zero-extend??? (MOVZX instruction on x86)
24 // I literally have no idea why cast/coersion doesn't work, union seems to work.
25 // But is it undefined behavior? And is there a better way to do it?
26 // I can't access StackOverflow rn so I'm leaving it like this, for now.
27 // I feel extremely stupid, feel free to make fun of me.
28 // Also, some cute stuff I found: 376825 (You can thank me later シ)
45 void *producer(__attribute__((unused)) void *arg)
47 // In a real world scenario, this would be something with actual latency,
48 // e.g. a network connection
49 // In this example, I'm creating the latency by allocating huge chunks of memory.
50 // Sometimes I kinda get the feeling I've fallen pretty low, but then I remember
51 // JavaScript programmers exist
52 // (DERZOMBIIIE IF YOU READ THIS IM SORRY NOTHING PERSONAL LUV U <3)
53 FILE *file = fopen("/dev/random", "r");
57 fread(buffer = malloc(CHUNK_SIZE), 1, CHUNK_SIZE, file);
58 while (queue_enq(&queue, buffer));
68 printf("-------------\n");
69 printf("Testing Queue\n");
70 printf("-------------\n");
72 printf("Random bullshit go: ");
75 pthread_t threads[17];
77 for (int i = 0; i < 17; i++)
78 pthread_create(&threads[i], NULL, i ? &consumer : &producer, NULL);
85 for (int i = 0; i < 17; i++)
86 pthread_join(threads[i], NULL);
92 // Btw I figured out my MOVZX problem from earlier, but it's funny so I'm leaving it
93 // I even figured it out without StackOverflow pls tell me you're proud of me
94 // The reason why char -> int sometimes extends to 0 and sometimes to F is this:
95 // char is signed by default, so if the first bit (sign bit) is on, it will extend that
96 // to the first bit on the int (MOVSX instruction)
97 // just use unsigned char to avoid it
99 // ngl was even tempted to use inline assembly this is what happens to your brain when
102 // for real tho, why tf is char signed by default
103 // its probably machine dependent (since there is the explicit signed qualifier for char)
105 // This file is probably the biggest bogus I've ever published anyway