C语言多线程教程(pthread)(线程创建pthread_t,指定线程run方法pthread_create,加mutex锁,解锁,伪共享 false sharing【假共享】)
[C語言]多線程程序入門教程
文章目錄
- 查看pthread_create()函數文檔
- · Demo1 單線程(創建線程pthread_t 、創建線程run方法pthread_create)
- · Demo2 雙線程(一個打印1,一個打印2)
- pthread_create()函數第四個參數,用于傳遞第三個參數中函數的參數使用(參數你可以傳遞任何類型,到時轉換成相應類型的指針即可)
- · Demo3 隨機5000個數字,線程一加前2500個,線程2加后2500個,然后兩個結果求和
- 我的垃圾實現- -
- 老師的實現(通過傳入結構體指針,獲取返回值結果)
- · Demo4 兩個線程同時提取數組元素相加(數組元素為1~5000)
- 不加鎖試試
- 加mutex鎖🔒(pthread_mutex_t lock;)
- pthread_mutex_init() 鎖初始化函數
- 代碼示例(加 / 解線程鎖:pthread_mutex_lock(&lock); pthread_mutex_unlock(&lock);)
- 假共享(偽共享)(False Sharing):在多核cpu中,因為多線程不同核緩存的不一致,需要同步導致的時間延誤?
thread線程和process進程:前者共享內存,后者不共享
查看pthread_create()函數文檔
man pthread_create PTHREAD_CREATE(3) Linux Programmer's Manual PTHREAD_CREATE(3)NAMEpthread_create - create a new threadSYNOPSIS#include <pthread.h>int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);Compile and link with -pthread.DESCRIPTIONThe pthread_create() function starts a new thread in the calling process. The new thread starts execution by invoking start_routine(); arg is passed as the sole argument of start_routine().The new thread terminates in one of the following ways:* It calls pthread_exit(3), specifying an exit status value that is available to another thread in the same process that calls pthread_join(3).* It returns from start_routine(). This is equivalent to calling pthread_exit(3) with the value supplied in the return statement.* It is canceled (see pthread_cancel(3)).* Any of the threads in the process calls exit(3), or the main thread performs a return from main(). This causes the termination of all threads in the process.The attr argument points to a pthread_attr_t structure whose contents are used at thread creation time to determine attributes for the new thread; this structure is initialized usingpthread_attr_init(3) and related functions. If attr is NULL, then the thread is created with default attributes.Before returning, a successful call to pthread_create() stores the ID of the new thread in the buffer pointed to by thread; this identifier is used to refer to the thread in subsequent callsto other pthreads functions.The new thread inherits a copy of the creating thread's signal mask (pthread_sigmask(3)). The set of pending signals for the new thread is empty (sigpending(2)). The new thread does notinherit the creating thread's alternate signal stack (sigaltstack(2)).The new thread inherits the calling thread's floating-point environment (fenv(3)).The initial value of the new thread's CPU-time clock is 0 (see pthread_getcpuclockid(3)).Linux-specific detailsThe new thread inherits copies of the calling thread's capability sets (see capabilities(7)) and CPU affinity mask (see sched_setaffinity(2)).RETURN VALUEOn success, pthread_create() returns 0; on error, it returns an error number, and the contents of *thread are undefined.ERRORSEAGAIN Insufficient resources to create another thread.EAGAIN A system-imposed limit on the number of threads was encountered. There are a number of limits that may trigger this error: the RLIMIT_NPROC soft resource limit (set via setrlimit(2)),which limits the number of processes and threads for a real user ID, was reached; the kernel's system-wide limit on the number of processes and threads, /proc/sys/kernel/threads-max,was reached (see proc(5)); or the maximum number of PIDs, /proc/sys/kernel/pid_max, was reached (see proc(5)).EINVAL Invalid settings in attr.EPERM No permission to set the scheduling policy and parameters specified in attr.ATTRIBUTESFor an explanation of the terms used in this section, see attributes(7).┌─────────────────┬───────────────┬─────────┐│Interface │ Attribute │ Value │├─────────────────┼───────────────┼─────────┤│pthread_create() │ Thread safety │ MT-Safe │└─────────────────┴───────────────┴─────────┘CONFORMING TOPOSIX.1-2001, POSIX.1-2008.NOTESSee pthread_self(3) for further information on the thread ID returned in *thread by pthread_create(). Unless real-time scheduling policies are being employed, after a call to pthread_cre‐ate(), it is indeterminate which thread—the caller or the new thread—will next execute.A thread may either be joinable or detached. If a thread is joinable, then another thread can call pthread_join(3) to wait for the thread to terminate and fetch its exit status. Only when aterminated joinable thread has been joined are the last of its resources released back to the system. When a detached thread terminates, its resources are automatically released back to thesystem: it is not possible to join with the thread in order to obtain its exit status. Making a thread detached is useful for some types of daemon threads whose exit status the applicationdoes not need to care about. By default, a new thread is created in a joinable state, unless attr was set to create the thread in a detached state (using pthread_attr_setdetachstate(3)).On Linux/x86-32, the default stack size for a new thread is 2 megabytes. Under the NPTL threading implementation, if the RLIMIT_STACK soft resource limit at the time the program started hasany value other than "unlimited", then it determines the default stack size of new threads. Using pthread_attr_setstacksize(3), the stack size attribute can be explicitly set in the attrargument used to create a thread, in order to obtain a stack size other than the default.BUGSIn the obsolete LinuxThreads implementation, each of the threads in a process has a different process ID. This is in violation of the POSIX threads specification, and is the source of manyother nonconformances to the standard; see pthreads(7).EXAMPLEThe program below demonstrates the use of pthread_create(), as well as a number of other functions in the pthreads API.In the following run, on a system providing the NPTL threading implementation, the stack size defaults to the value given by the "stack size" resource limit:$ ulimit -s8192 # The stack size limit is 8 MB (0x800000 bytes)$ ./a.out hola salut servusThread 1: top of stack near 0xb7dd03b8; argv_string=holaThread 2: top of stack near 0xb75cf3b8; argv_string=salutThread 3: top of stack near 0xb6dce3b8; argv_string=servusJoined with thread 1; returned value was HOLAJoined with thread 2; returned value was SALUTJoined with thread 3; returned value was SERVUSIn the next run, the program explicitly sets a stack size of 1MB (using pthread_attr_setstacksize(3)) for the created threads:$ ./a.out -s 0x100000 hola salut servusThread 1: top of stack near 0xb7d723b8; argv_string=holaThread 2: top of stack near 0xb7c713b8; argv_string=salutThread 3: top of stack near 0xb7b703b8; argv_string=servusJoined with thread 1; returned value was HOLAJoined with thread 2; returned value was SALUTJoined with thread 3; returned value was SERVUSProgram source#include <pthread.h>#include <string.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <ctype.h>#define handle_error_en(en, msg) \do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)#define handle_error(msg) \do { perror(msg); exit(EXIT_FAILURE); } while (0)struct thread_info { /* Used as argument to thread_start() */pthread_t thread_id; /* ID returned by pthread_create() */int thread_num; /* Application-defined thread # */char *argv_string; /* From command-line argument */};/* Thread start function: display address near top of our stack,and return upper-cased copy of argv_string */static void *thread_start(void *arg){struct thread_info *tinfo = arg;char *uargv, *p;printf("Thread %d: top of stack near %p; argv_string=%s\n",tinfo->thread_num, &p, tinfo->argv_string);uargv = strdup(tinfo->argv_string);if (uargv == NULL)handle_error("strdup");for (p = uargv; *p != '\0'; p++)*p = toupper(*p);return uargv;}intmain(int argc, char *argv[]){int s, tnum, opt, num_threads;struct thread_info *tinfo;pthread_attr_t attr;int stack_size;void *res;/* The "-s" option specifies a stack size for our threads */stack_size = -1;while ((opt = getopt(argc, argv, "s:")) != -1) {switch (opt) {case 's':stack_size = strtoul(optarg, NULL, 0);break;default:fprintf(stderr, "Usage: %s [-s stack-size] arg...\n",argv[0]);exit(EXIT_FAILURE);}}num_threads = argc - optind;/* Initialize thread creation attributes */s = pthread_attr_init(&attr);if (s != 0)handle_error_en(s, "pthread_attr_init");if (stack_size > 0) {s = pthread_attr_setstacksize(&attr, stack_size);if (s != 0)handle_error_en(s, "pthread_attr_setstacksize");}/* Allocate memory for pthread_create() arguments */tinfo = calloc(num_threads, sizeof(struct thread_info));if (tinfo == NULL)handle_error("calloc");/* Create one thread for each command-line argument */for (tnum = 0; tnum < num_threads; tnum++) {tinfo[tnum].thread_num = tnum + 1;tinfo[tnum].argv_string = argv[optind + tnum];/* The pthread_create() call stores the thread ID intocorresponding element of tinfo[] */s = pthread_create(&tinfo[tnum].thread_id, &attr,&thread_start, &tinfo[tnum]);if (s != 0)handle_error_en(s, "pthread_create");}/* Destroy the thread attributes object, since it is nolonger needed */s = pthread_attr_destroy(&attr);if (s != 0)handle_error_en(s, "pthread_attr_destroy");/* Now join with each thread, and display its returned value */for (tnum = 0; tnum < num_threads; tnum++) {s = pthread_join(tinfo[tnum].thread_id, &res);if (s != 0)handle_error_en(s, "pthread_join");printf("Joined with thread %d; returned value was %s\n",tinfo[tnum].thread_num, (char *) res);free(res); /* Free memory allocated by thread */}free(tinfo);exit(EXIT_SUCCESS);}SEE ALSOgetrlimit(2), pthread_attr_init(3), pthread_cancel(3), pthread_detach(3), pthread_equal(3), pthread_exit(3), pthread_getattr_np(3), pthread_join(3), pthread_self(3), pthreads(7)COLOPHONThis page is part of release 4.04 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found athttp://www.kernel.org/doc/man-pages/.Linux 2015-07-23 PTHREAD_CREATE(3)· Demo1 單線程(創建線程pthread_t 、創建線程run方法pthread_create)
#include <stdio.h> #include <stdlib.h> #include <pthread.h>void* myfunc(void* args){printf("Hello World\n");return NULL; }int main(){pthread_t th;pthread_create(&th, NULL, myfunc, NULL); //第一個參數是線程th的地址,第三個參數是指針函數的名字(是不是函數的指針不知道)pthread_join(th, NULL); //等待線程th結束return 0; }編譯運行:
[yg@ubuntu ~/arnold_test/20211013_pthread_test]5$ gcc pthread_test1.c -lpthread [yg@ubuntu ~/arnold_test/20211013_pthread_test]6$ [yg@ubuntu ~/arnold_test/20211013_pthread_test]6$ [yg@ubuntu ~/arnold_test/20211013_pthread_test]6$ ./a.out Hello World· Demo2 雙線程(一個打印1,一個打印2)
#include <stdio.h> #include <stdlib.h> #include <pthread.h>void* myfunc1(void* args){for(int i = 1; i<1000000; i++){printf("%d", 1);}return NULL; }void* myfunc2(void* args){for(int i = 1; i<1000000; i++){printf("%d", 2);}return NULL; }int main(){pthread_t th1;pthread_t th2;pthread_create(&th1, NULL, myfunc1, NULL); //第一個參數是線程th的地址,第三個參數是指針函數的名字(是不是函數的指針不知道)pthread_create(&th2, NULL, myfunc2, NULL);pthread_join(th1, NULL); //等待線程th結束,注意這里第一個參數不用地址pthread_join(th2, NULL);return 0; }編譯運行:
[yg@ubuntu ~/arnold_test/20211013_pthread_test]5$ gcc pthread_test1.c -lpthread [yg@ubuntu ~/arnold_test/20211013_pthread_test]6$ [yg@ubuntu ~/arnold_test/20211013_pthread_test]6$ [yg@ubuntu ~/arnold_test/20211013_pthread_test]6$ ./a.out運行結果:可以看到,是交替運行的
pthread_create()函數第四個參數,用于傳遞第三個參數中函數的參數使用(參數你可以傳遞任何類型,到時轉換成相應類型的指針即可)
#include <stdio.h> #include <stdlib.h> #include <pthread.h>void* myfunc(void* args){char* name = (char*)args;for(int i = 1; i<10000; i++){printf("%s:%d\t", name, i);}return NULL; }int main(){pthread_t th1;pthread_t th2;pthread_create(&th1, NULL, myfunc, "th1"); //第一個參數是線程th的地址,第三個參數是指針函數的名字(是不是函數的指針不知道)pthread_create(&th2, NULL, myfunc, "th2");pthread_join(th1, NULL); //等待線程th結束,注意這里第一個參數不用地址pthread_join(th2, NULL);return 0; }編譯運行:
[yg@ubuntu ~/arnold_test/20211013_pthread_test]5$ gcc pthread_test1.c -lpthread [yg@ubuntu ~/arnold_test/20211013_pthread_test]6$ [yg@ubuntu ~/arnold_test/20211013_pthread_test]6$ [yg@ubuntu ~/arnold_test/20211013_pthread_test]6$ ./a.out運行結果:也可以看到有交替的
· Demo3 隨機5000個數字,線程一加前2500個,線程2加后2500個,然后兩個結果求和
我的垃圾實現- -
#include <stdio.h> #include <stdlib.h> #include <pthread.h>int arr[5000]; //創建一個包含大小為5000的整型數組 int s1; //前2500個數字和,分配給線程1去加 int s2; //后2500個數字和,分配給線程2去加void* myfunc(void* args){int index = *(int*)args;//printf("%d\n", index);int count = 0;int sum = 0;while(count<2500){sum+=arr[index];index++;count++;}if(index-2500==0){s1+=sum;}else{s2+=sum;}return NULL; }int main(){//設置隨機數種子(否則每次都一樣結果)srand(time(0));int i;for(i=0;i<5000;i++){//給數組賦值arr[i]=rand()%50; //rand()特別大,所以要取余//printf("%d\n", arr[i]);//arr[i]=i+1; //測試1到5000和是12502500,沒問題//printf("%d\n",arr[i]);}pthread_t th1;pthread_t th2;int a = 0;int b = 2500;pthread_create(&th1, NULL, myfunc, &a); pthread_create(&th2, NULL, myfunc, &b);pthread_join(th1, NULL); pthread_join(th2, NULL);printf("s1 = %d, s2 = %d\n", s1, s2);return 0; }編譯運行結果:因為是對50取余,所以每個結果接近(0~49)* 2500 = 24.5 * 2500 = 61250
[yg@ubuntu ~/arnold_test/20211013_pthread_test]127$ gcc pthread_test1.c -lpthread [yg@ubuntu ~/arnold_test/20211013_pthread_test]128$ ./a.out s1 = 60163, s2 = 61379 [yg@ubuntu ~/arnold_test/20211013_pthread_test]129$ ./a.out s1 = 62296, s2 = 60521 [yg@ubuntu ~/arnold_test/20211013_pthread_test]130$ ./a.out s1 = 61066, s2 = 61084 [yg@ubuntu ~/arnold_test/20211013_pthread_test]131$ ./a.out s1 = 61051, s2 = 61582 [yg@ubuntu ~/arnold_test/20211013_pthread_test]132$老師的實現(通過傳入結構體指針,獲取返回值結果)
1、可以為每個線程單獨寫一個函數,這樣雖然簡答,但代碼重復多
2、只寫一個函數,在外部定義包含需要相加的數字下標起止點的結構體,同時在結構體中定義接收計算結果的變量,將結構體作為線程函數的第四個參數傳入
代碼實現:
#include <stdio.h> #include <stdlib.h> #include <pthread.h>int arr[5000]; //創建一個包含大小為5000的整型數組 int s1; //前2500個數字和,分配給線程1去加 int s2; //后2500個數字和,分配給線程2去加//typedef struct _MY_ARGS{ typedef struct{int start;int end;int result; }MY_ARGS;void* myfunc(void* args){MY_ARGS* p = (MY_ARGS*)args;int sum = 0; //不要忘記賦初始值(!)int i;for(i= p->start; i < p->end; i++){sum+=arr[i];//printf("%d\n", arr[i]);}p->result = sum;return NULL; }int main(){//設置隨機數種子(否則每次都一樣結果)srand(time(0));int i;for(i=0;i<5000;i++){//給數組賦值arr[i]=rand()%50; //rand()特別大,所以要取余//printf("%d\n", arr[i]);//arr[i]=i+1; //測試1到5000和是12502500,沒問題//printf("%d\n",arr[i]);}pthread_t th1;pthread_t th2;MY_ARGS args1 = {0, 2500, 0};MY_ARGS args2 = {2500, 5000, 0};pthread_create(&th1, NULL, myfunc, &args1); pthread_create(&th2, NULL, myfunc, &args2);pthread_join(th1, NULL); pthread_join(th2, NULL);printf("s1 = %d, s2 = %d\n", args1.result, args2.result);return 0; }編譯運行結果:
[yg@ubuntu ~/arnold_test/20211013_pthread_test]182$ gcc pthread_test1.c -lpthread [yg@ubuntu ~/arnold_test/20211013_pthread_test]183$ ./a.out s1 = 63337, s2 = 59578 [yg@ubuntu ~/arnold_test/20211013_pthread_test]184$ ./a.out s1 = 60652, s2 = 60818 [yg@ubuntu ~/arnold_test/20211013_pthread_test]185$ ./a.out s1 = 61265, s2 = 62009 [yg@ubuntu ~/arnold_test/20211013_pthread_test]186$ ./a.out s1 = 61265, s2 = 62009 [yg@ubuntu ~/arnold_test/20211013_pthread_test]187$ ./a.out s1 = 61912, s2 = 62479 [yg@ubuntu ~/arnold_test/20211013_pthread_test]188$· Demo4 兩個線程同時提取數組元素相加(數組元素為1~5000)
不加鎖試試
#include <stdio.h> #include <stdlib.h> #include <pthread.h>int arr[5000]; //創建一個包含大小為5000的整型數組 int sum = 0; //兩個線程同時對sum進行操作//typedef struct _MY_ARGS{ typedef struct{int start;int end; }MY_ARGS;void* myfunc(void* args){MY_ARGS* p = (MY_ARGS*)args;int i;for(i= p->start; i < p->end; i++){sum+=arr[i];//printf("%d\n", arr[i]);}return NULL; }int main(){//設置隨機數種子(否則每次都一樣結果)srand(time(0));int i;for(i=0;i<5000;i++){//給數組賦值//arr[i]=rand()%50; //rand()特別大,所以要取余//printf("%d\n", arr[i]);arr[i]=i+1; //測試1到5000和是12502500,沒問題//printf("%d\n",arr[i]);}pthread_t th1;pthread_t th2;MY_ARGS args1 = {0, 2500};MY_ARGS args2 = {2500, 5000};pthread_create(&th1, NULL, myfunc, &args1); pthread_create(&th2, NULL, myfunc, &args2);pthread_join(th1, NULL); pthread_join(th2, NULL);printf("sum = %d", sum);return 0; }運行編譯結果:
[yg@ubuntu ~/arnold_test/20211013_pthread_test]207$ gcc pthread_test1.c -lpthread [yg@ubuntu ~/arnold_test/20211013_pthread_test]208$ ./a.out sum = 12502500[yg@ubuntu ~/arnold_test/20211013_pthread_test]209$ ./a.out sum = 12502500[yg@ubuntu ~/arnold_test/20211013_pthread_test]210$ ./a.out sum = 12502500[yg@ubuntu ~/arnold_test/20211013_pthread_test]211$ ./a.out sum = 12502500[yg@ubuntu ~/arnold_test/20211013_pthread_test]212$ ./a.out sum = 12502500[yg@ubuntu ~/arnold_test/20211013_pthread_test]213$ ./a.out可以看到,結果不全盡然是12502500,說明兩個線程其中一個在把元素加到sum變量的同時,另一個線程也在把另一個元素往sum里面加,沒排隊按順序加,導致結果不對
加mutex鎖🔒(pthread_mutex_t lock;)
pthread_mutex_init() 鎖初始化函數
代碼示例(加 / 解線程鎖:pthread_mutex_lock(&lock); pthread_mutex_unlock(&lock);)
#include <stdio.h> #include <stdlib.h> #include <pthread.h>int arr[5000]; //創建一個包含大小為5000的整型數組 int sum = 0; //兩個線程同時對sum進行操作pthread_mutex_t lock; //創建mutex鎖🔒//typedef struct _MY_ARGS{ typedef struct{int start;int end; }MY_ARGS;void* myfunc(void* args){MY_ARGS* p = (MY_ARGS*)args;int i;//int a;for(i= p->start; i < p->end; i++){pthread_mutex_lock(&lock); //鎖住代碼//a=sum;sum+=arr[i];//printf("%d", sum-a-arr[i]); //驗證不加鎖時會不按順序亂加sum(如果出現不為0的數表示線程亂竄了)pthread_mutex_unlock(&lock); //解鎖代碼//printf("%d\n", arr[i]);}return NULL; }int main(){//設置隨機數種子(否則每次都一樣結果)srand(time(0));int i;for(i=0;i<5000;i++){//給數組賦值//arr[i]=rand()%50; //rand()特別大,所以要取余//printf("%d\n", arr[i]);arr[i]=i+1; //測試1到5000和是12502500,沒問題//printf("%d\n",arr[i]);}pthread_t th1;pthread_t th2;pthread_mutex_init(&lock, NULL); //初始化mutex鎖🔒MY_ARGS args1 = {0, 2500};MY_ARGS args2 = {2500, 5000};pthread_create(&th1, NULL, myfunc, &args1); pthread_create(&th2, NULL, myfunc, &args2);pthread_join(th1, NULL); pthread_join(th2, NULL);printf("sum = %d", sum);return 0; }運行編譯結果:
[yg@ubuntu ~/arnold_test/20211013_pthread_test]346$ gcc pthread_tedt1.c -lpthread [yg@ubuntu ~/arnold_test/20211013_pthread_test]347$ ./a.out sum = 12502500[yg@ubuntu ~/arnold_test/20211013_pthread_test]348$ ./a.out sum = 12502500[yg@ubuntu ~/arnold_test/20211013_pthread_test]349$ ./a.out sum = 12502500[yg@ubuntu ~/arnold_test/20211013_pthread_test]350$ ./a.out sum = 12502500[yg@ubuntu ~/arnold_test/20211013_pthread_test]351$ ./a.out sum = 12502500[yg@ubuntu ~/arnold_test/20211013_pthread_test]352$查看運行時間:看不出來,數字太小了(反正作者的意思就是,加鎖解鎖也需要消費時間!)
[yg@ubuntu ~/arnold_test/20211013_pthread_test]352$ time ./a.out sum = 12502500 real 0m0.001s user 0m0.001s sys 0m0.000s假共享(偽共享)(False Sharing):在多核cpu中,因為多線程不同核緩存的不一致,需要同步導致的時間延誤?
https://www.bilibili.com/video/BV1kt411z7ND?p=4
這里面作者把兩個線程計算結果分別存到同一個整型數組的連續兩個位置,長度較短,于是RAM復制到Cache緩存的時候就會整體復制過去,,同步時就會產生較大的時間延誤
怎么讓它不整體復制呢,方法就是增加字符數組長度(增大到它不想整體復制為止),比如將第一個結果存到第一個int下,將第二個結果存到第101個int下,
總結
以上是生活随笔為你收集整理的C语言多线程教程(pthread)(线程创建pthread_t,指定线程run方法pthread_create,加mutex锁,解锁,伪共享 false sharing【假共享】)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cmake-变量作用域
- 下一篇: C语言 (条件编译#ifdef、#ifn