c: thread in Ubuntu
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 | /** * @file helloworld.c * @author your name (geovindu) * @brief * ide: vscode c11,c17 * @version 0.1 * @date 2023-10-21 * * @copyright Copyright (c) 2023 站在巨人的肩膀上 Standing on the Shoulders of Giants * */ #include<stdlib.h> #include<stdio.h> #include<ctype.h> #include<string.h> #include<malloc.h> #include<time.h> #include <unistd.h> #include <sys/wait.h> #include<threads.h> #include<math.h> #include "include/CheckTieck.h" #include "include/TakeNumber.h" // #define threadcout 5 // thrd_t threadId[threadcout]; // size_t task=0; mtx_t task_mtx; /** * @brief * */ struct timespec duration={.tv_sec=1,.tv_nsec=0}; /** * @brief 线程 * * @param agr * @return int */ int execrteTask( void *agr) { mtx_lock(&task_mtx); size_t local_task = ++task; mtx_unlock(&task_mtx); // mtx_lock(&task_mtx); // mutex lock - blocks until acquired // printf_s("Task %zd started.\n", ++task); printf ( "Task %zd started.\n" , local_task); thrd_sleep(&duration, NULL); // Just to make things take longer... double x = 0; for ( int i = 0 ; i< 1000000000 ; ++i) x = sqrt (3.1415926); printf ( " Task %zd finished\n" , local_task); // printf_s(" Task %zd finished\n", task); // mtx_unlock(&task_mtx); // mutex unlock - for use by other threads return 0; } /** * @brief * * @return int */ int main( void ) { if (thrd_error == mtx_init(&task_mtx, mtx_timed)) { fprintf (stderr, "Mutex creation failed.\n" ); thrd_exit(-2); } // Create the threads to carry out the tasks concurrently for ( size_t i = 0 ; i<threadcout ; ++i) if (thrd_error == thrd_create(&(threadId[i]), execrteTask, NULL)) { fprintf (stderr, "Thread creation failed.\n" ); thrd_exit(-1); } // Join the additional threads to the main thread for ( size_t j = 0 ; j <threadcout ; ++j) thrd_join(threadId[j], NULL); pid_t pid; int status; pid = fork(); // 创建一个新进程 if (pid < 0) { // 如果创建失败,输出错误信息 fprintf (stderr, "Fork Failed" ); return 1; } else if (pid == 0) { // 子进程 printf ( "I am the child %d\n" ,pid); execl( "/bin/ls" , "ls" , NULL); // 在子进程中执行 /bin/ls 程序 printf ( "I am the child %d, and execl failed\n" ,pid); // 如果 execl 返回,那么说明出错 } else { // 父进程 wait(&status); // 等待子进程结束 printf ( "I am the parent %d, and my child has ended\n" ,pid); } printf ( "hello wolrd, c launguage! weblcome geovindu!涂聚文" ); QueueCalling *queue1; char select= '1' ; //int num=1;//顾客序号 int num=0; //叫号编号 queue1=QueueInit(); //初始化队列 if (queue1==NULL) { printf ( "创建队列时出错!\n" ); //getch(); getchar (); return 0; } do { //这里处理,列表不会显示两次 if (select== '1' || select== '2' ) { printf ( "\n请选择具体操作:\n" ); printf ( "1.新到顾客\n" ); printf ( "2.下一个顾客\n" ); printf ( "0.退出\n" ) ; fflush (stdin); } select= getchar (); //getch(); switch (select) { case '1' : add(queue1); printf ( "\n现在共有%d位顾客在等候!\n" ,QueueLen(queue1)); break ; case '2' : next(queue1); printf ( "\n现在共有%d位顾客在等候!\n" ,QueueLen(queue1)); break ; case '0' : break ; } } while (select!= '0' ); QueueFree(queue1); //释放队列 //getch(); getchar (); return 0; } |
6.5. Thread Arguments and Return Values — Computer Systems Fundamentals (jmu.edu)
https://w3.cs.jmu.edu/kirkpams/OpenCSF/Books/csf/html/ThreadArgs.html
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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | /** * @file helloworld.c * @author your name (geovindu) * @brief * IDE vscode Ubuntu 20. c11,c17 * @version 0.1 * @date 2023-10-20 * https://www.devsurvival.com/multi-threaded-programming/ * @copyright Copyright (c) 2023 站在巨人的肩膀上 Standing on the Shoulders of Giants * */ #include<stdlib.h> #include<stdio.h> #include<ctype.h> #include<string.h> #include<malloc.h> #include<time.h> #include<unistd.h> #include<sys/wait.h> #include<threads.h> #include<math.h> #include<pthread.h> #include <semaphore.h> #include "include/CheckTieck.h" #include "include/TakeNumber.h" // #define threadcout 5 // thrd_t threadId[threadcout]; // size_t task=0; mtx_t task_mtx; /** * @brief * */ struct timespec duration={.tv_sec=1,.tv_nsec=0}; int sum = 0; // Global int to store the sum of the two arrays sem_t mutex; // Synchronization Bit /** * @brief add number * * @param arg * @return void* */ void *addNumber( void *arg){ int *ptr = ( int *) arg; while (*ptr != -1){ sem_wait(&mutex); sum += *ptr; printf ( "value: %d sum %d\n" , *ptr,sum ); sem_post(&mutex); ptr++; } return NULL; } /** * @brief * */ struct numbers { int a; int b; int sum; }; /** * @brief * * @param _args * @return void* */ void *sum_thread ( void *_args) { /* Cast the arguments to the usable struct type */ struct numbers *args = ( struct numbers *) _args; /* Place the result into the struct itself (on the heap) */ args->sum = args->a + args->b; printf ( "\nsum=%d\n" ,args->sum); pthread_exit (NULL); } struct args { int a; int b; }; /* struct for returning results from the child thread */ struct results { int sum; int difference; int product; int quotient; int modulus; }; /** * @brief * * @param _args * @return struct results* */ struct results *calculator ( void *_args) { /* Cast the args to the usable struct type */ struct args *args = ( struct args *) _args; /* Allocate heap space for this thread's results */ struct results *results = calloc ( sizeof ( struct results), 1); results->sum = args->a + args->b; results->difference = args->a - args->b; results->product = args->a * args->b; results->quotient = args->a / args->b; results->modulus = args->a % args->b; printf ( "\nproduct:=%d\n" ,results->product); /* De-allocate the input instance and return the pointer to results on heap */ //free (args); //pthread_exit (results); //退出线程 pthread_exit (NULL); return results; } /** * @brief 线程 * * @param agr * @return int */ int execrteTask( void *agr) { mtx_lock(&task_mtx); size_t local_task = ++task; mtx_unlock(&task_mtx); // mtx_lock(&task_mtx); // mutex lock - blocks until acquired // printf_s("Task %zd started.\n", ++task); printf ( "Task %zd started.\n" , local_task); thrd_sleep(&duration, NULL); // Just to make things take longer... double x = 0; for ( int i = 0 ; i< 1000000000 ; ++i) x = sqrt (3.1415926); printf ( " Task %zd finished\n" , local_task); // printf_s(" Task %zd finished\n", task); // mtx_unlock(&task_mtx); // mutex unlock - for use by other threads return 0; } /** * @brief * * @return int */ int main( void ) { int A[4] = {1,5,3, -1}; // -1 marks the end of array int B[4] = {4,9,6, -1}; pthread_t t_a, t_b; sem_init(&mutex, 0, 1); pthread_create(&t_a , NULL, addNumber, A); pthread_create(&t_b, NULL, addNumber, B); pthread_join(t_a, NULL); pthread_join(t_b, NULL); printf ( "Total: %d\n" , sum); int i; pthread_t tid; // Let us create three threads for (i = 0; i < 3; i++) { pthread_create(&tid, NULL, calculator, ( void *)&tid); pthread_create(&tid, NULL, sum_thread, ( void *)&tid); } //pthread_exit(NULL); //退出线程 if (thrd_error == mtx_init(&task_mtx, mtx_timed)) { fprintf (stderr, "Mutex creation failed.\n" ); thrd_exit(-2); } // Create the threads to carry out the tasks concurrently for ( size_t i = 0 ; i<threadcout ; ++i) if (thrd_error == thrd_create(&(threadId[i]), execrteTask, NULL)) { fprintf (stderr, "Thread creation failed.\n" ); thrd_exit(-1); } // Join the additional threads to the main thread for ( size_t j = 0 ; j <threadcout ; ++j) thrd_join(threadId[j], NULL); printf ( "hello wolrd, c launguage! welcome geovindu!涂聚文" ); QueueCalling *queue1; char select= '1' ; //int num=1;//顾客序号 int num=0; //叫号编号 queue1=QueueInit(); //初始化队列 if (queue1==NULL) { printf ( "创建队列时出错!\n" ); //getch(); getchar (); return 0; } do { //这样处理,不会显示两次选择列表 if (select== '1' || select== '2' ) { printf ( "\n请选择具体操作:\n" ); printf ( "1.新到顾客\n" ); printf ( "2.下一个顾客\n" ); printf ( "0.退出\n" ) ; fflush (stdin); } select= getchar (); //getch(); switch (select) { case '1' : add(queue1); printf ( "\n现在共有%d位顾客在等候!\n" ,QueueLen(queue1)); break ; case '2' : next(queue1); printf ( "\n现在共有%d位顾客在等候!\n" ,QueueLen(queue1)); break ; case '0' : break ; } } while (select!= '0' ); QueueFree(queue1); //释放队列 //getch(); getchar (); return 0; } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Cpp programming
标签:
线程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2022-10-22 Python: Proxy Pattern
2022-10-22 CSharp: Open Source QRCode Library
2022-10-22 Python: Flyweight Pattern
2022-10-22 Python: Facade Pattern
2009-10-22 Top 126 Ajax Tutorials