1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
void demo_event_driven_architecture() { printf("=== 事件驱动架构演示 ===\n"); event_loop_t *loop = event_loop_create(100); if (!loop) { printf("Failed to create event loop\n"); return; } event_loop_add_listener(loop, EVENT_TIMER, timer_handler, NULL, 0); event_loop_add_listener(loop, EVENT_NETWORK, network_handler, NULL, 0); event_loop_add_listener(loop, EVENT_USER, user_handler, NULL, 0); pthread_t loop_thread; pthread_create(&loop_thread, NULL, (void*(*)(void*))event_loop_run, loop); for (int i = 0; i < 5; i++) { event_t event = {0}; event.timestamp = time(NULL); switch (i % 3) { case 0: event.type = EVENT_TIMER; printf("Posting timer event %d\n", i); break; case 1: { event.type = EVENT_NETWORK; const char *msg = "Hello Network!"; event.data = strdup(msg); event.data_size = strlen(msg); printf("Posting network event %d\n", i); break; } case 2: event.type = EVENT_USER; event.data = malloc(sizeof(int)); *(int*)event.data = i; event.data_size = sizeof(int); printf("Posting user event %d\n", i); break; } event_loop_post(loop, &event); sleep(1); } sleep(2); event_loop_stop(loop); pthread_join(loop_thread, NULL); event_loop_destroy(loop); printf("=== 演示完成 ===\n\n"); }
void demo_lockfree_queue() { printf("=== 无锁队列演示 ===\n"); queue = lockfree_queue_create(); atomic_init(&items_produced, 0); atomic_init(&items_consumed, 0); if (!queue) { printf("Failed to create queue\n"); return; } pthread_t producers[NUM_PRODUCERS]; pthread_t consumers[NUM_CONSUMERS]; int producer_ids[NUM_PRODUCERS]; int consumer_ids[NUM_CONSUMERS]; for (int i = 0; i < NUM_PRODUCERS; i++) { producer_ids[i] = i; pthread_create(&producers[i], NULL, producer_thread, &producer_ids[i]); } for (int i = 0; i < NUM_CONSUMERS; i++) { consumer_ids[i] = i; pthread_create(&consumers[i], NULL, consumer_thread, &consumer_ids[i]); } for (int i = 0; i < NUM_PRODUCERS; i++) { pthread_join(producers[i], NULL); } for (int i = 0; i < NUM_CONSUMERS; i++) { pthread_join(consumers[i], NULL); } printf("Total produced: %d\n", atomic_load(&items_produced)); printf("Total consumed: %d\n", atomic_load(&items_consumed)); printf("Queue size: %zu\n", lockfree_queue_size(queue)); lockfree_queue_destroy(queue); printf("=== 演示完成 ===\n\n"); }
void demo_cache_friendly_structures() { printf("=== 缓存友好数据结构演示 ===\n"); size_t test_sizes[] = {1000, 10000, 100000, 1000000}; int num_tests = sizeof(test_sizes) / sizeof(test_sizes[0]); printf("%-10s %-12s %-12s %-10s\n", "Elements", "SoA Time(s)", "AoS Time(s)", "Speedup"); printf("------------------------------------------------\n"); for (int i = 0; i < num_tests; i++) { performance_result_t result = test_performance(test_sizes[i]); double speedup = result.aos_time / result.soa_time; printf("%-10zu %-12.6f %-12.6f %-10.2fx\n", result.elements_processed, result.soa_time, result.aos_time, speedup); } printf("=== 演示完成 ===\n\n"); }
void demo_secure_strings() { printf("=== 安全字符串操作演示 ===\n"); secure_string_t *str1 = secure_string_create(20, 1); secure_string_t *str2 = secure_string_from_cstr("Hello", 1); if (!str1 || !str2) { printf("Failed to create secure strings\n"); return; } printf("Initial strings:\n"); printf("str1: '%s' (length: %zu)\n", secure_string_cstr(str1), secure_string_length(str1)); printf("str2: '%s' (length: %zu)\n", secure_string_cstr(str2), secure_string_length(str2)); if (secure_string_append(str2, " World!") == 0) { printf("After append: '%s'\n", secure_string_cstr(str2)); } else { printf("Append failed (security check)\n"); } if (secure_string_printf(str1, "Number: %d, String: %s", 42, "test") >= 0) { printf("Formatted string: '%s'\n", secure_string_cstr(str1)); } else { printf("Format failed (security check)\n"); } printf("\nTesting security checks:\n"); if (secure_string_append(str1, "This is a very long string that exceeds capacity") == -1) { printf("Security check prevented buffer overflow!\n"); } secure_string_t *str3 = secure_string_from_cstr("Hello World!", 1); printf("Comparison result: %d\n", secure_string_compare(str2, str3)); secure_string_destroy(str1); secure_string_destroy(str2); secure_string_destroy(str3); printf("=== 演示完成 ===\n\n"); }
void run_all_demos() { printf("C语言高级编程技巧演示\n"); printf("=====================\n\n"); demo_event_driven_architecture(); demo_lockfree_queue(); demo_cache_friendly_structures(); demo_secure_strings(); printf("所有演示完成!\n"); }
|