[linux小心得]pthread小記錄

大家好  小弟又來嘴砲了。

紀錄一下 最近在測試書本 關於pthread範例,雖然此書 有些地方有打錯,
不過整體概念都算正確的。

先來一個 印出thread的id example
#include #include #include /*thread ID, print process ID & thread ID*/ void * thfn(void * arg) { pid_t pid; pthread_t tid; pid = getpid(); tid = pthread_self(); /*Two ID change to number print*/ printf("the new thread: pid is: %u,tid is: %u\n", (unsigned int)pid,(unsigned int)tid); return NULL; } int main(void) { pid_t pid; int err; pthread_t tid, mtid; //int err; pid = getpid(); mtid = pthread_self(); err = pthread_create(&tid, NULL, thfn, NULL); /*error number same with errno,can use strerror function insert the easy language*/ if(err != 0){ /*can not use perror function,because error number not in errno variable value*/ printf("can't create thread %s\n", strerror(err)); exit(1); } /*sleep 1s,for check new thread be call in main thread before*/ sleep(1); /*print main thread ID & process ID*/ printf("the main thread: pid is: %u, tid is: %u\n",(unsigned int)pid,(unsigned int)mtid); return 0; } 再來是有把struct 參數傳進去thread function的方法。 #include #include #include typedef struct arg_struct ARG; /*variable struct * arg1:string * arg2:int * arg3:float * use typedef the arg_struct type define ARG * */ struct arg_struct{ char arg1[10]; int arg2; float arg3; }; /*thread,print the variable*/ void * thfn(void * arg) { ARG *p = (ARG *)arg; /*print three variable*/ printf("arg1 is : %s, arg2 is : %d, arg3 is : %f\n", p->arg1, p->arg2, p->arg3); return NULL; } int main(int argc,char *argv[]) { pthread_t tid; ARG arg; int err; /*give the three variable the value,struct should be define before create thread,create thread should be send the variable struct*/ strcpy(arg.arg1, argv[1]); arg.arg2 = atoi(argv[2]); arg.arg3 = atof(argv[3]); printf("arg1 is : %s, arg2 is : %d, arg3 is : %f\n", arg.arg1, arg.arg2, arg.arg3); err = pthread_create(&tid, NULL, thfn, (void *)&arg); /*create thread*/ if(err != 0){ printf("can't create thread %s\n", strerror(err)); exit(1); } pthread_join(tid, NULL); //return 0; } 下面這個證明 thread 存取資源的限制,一開始執行的時候 用cat test.txt是看不到東西的。 等執行完後才有 #include #include #include /*variable struct,a heap pointer with process variable pointer to new thread*/ typedef struct arg_struct{ int * heap; /*Heap data's pointer*/ int * stack; /*stack data's pointer*/ }ARG; FILE *fp = NULL; /*data object pointer,include stream buffer and data I/O*/ /*Process thread,change process Heap and Stack data value*/ void * thfn(void * arg) { ARG * p; p = (ARG *)arg; (*p->heap)++; (*p->stack)++; fprintf(fp, "new thread heap:%d stack :%d\n", *(p->heap), *(p->stack)); printf("the new thread done\n"); return NULL; } int main(void) { pthread_t tid, tid2; ARG arg; int * heap; int stack, err; heap = (int *)malloc(1 * sizeof(int)); /*allocate the heap space*/ if(heap == NULL){ perror("fail to malloc"); exit(1); } *heap = 2; stack = 3; /*Stack data orgin 3 */ /**/ arg.heap = heap; arg.stack = &stack; if( (fp = fopen("test.txt", "wb")) == NULL){ perror("fail to open\n"); exit(1); } err = pthread_create(&tid, NULL, thfn, (void *)&arg); /*create thread*/ if(err != 0){ printf("can't create thread %s\n", strerror(err)); exit(1); } sleep(10); (*heap)++; /*main thread ++ the heap & the Stack*/ stack++; fprintf(fp, "main thread: heap:%d stack :%d\n", *(arg.heap), *(arg.stack)); printf("the main thread done\n"); fclose(fp); free(heap); /*release the heap*/ return 0; } 以上下台一鞠躬 感謝收看

留言