赞
踩
线程库不属于标准库所以编译链接的时候需要指定线程库 -pthread
#include <pthread.h> #include <stdio.h> #include <unistd.h> #include <string.h> // callback function void* callback(void* arg) { printf("child thread...\n"); printf("arg value: %d\n", *(int *)arg); } int main() { pthread_t tid; int num = 10; // create a child thread int ret = pthread_create(&tid, NULL, callback, (void *)&num); if(ret != 0) { char* errstr = strerror(ret); printf("error: %s\n", errstr); } sleep(1); // what will happen, if delete this statement return 0; }
void pthread_exit(void *retval)
pthread_t pthread_self(void)
#include <pthread.h> #include <stdio.h> #include <unistd.h> #include <string.h> // callback function void* callback(void* arg) { sleep(1); printf("child thread...\n"); printf("arg value: %d\n", *(int *)arg); pthread_exit(0); // return 0; } int main() { pthread_t tid; int num = 10; // create a child thread int ret = pthread_create(&tid, NULL, callback, (void *)&num); if(ret != 0) { char* errstr = strerror(ret); printf("error: %s\n", errstr); } printf("tid: %ld, main thread id: %ld\n", tid, pthread_self()); // 终止一个线程 // 不会带走其子线程 pthread_exit(NULL); printf("main thread exit\n"); return 0; }
int pthread_join(pthread_t thread, void **retval);
#include <pthread.h> #include <stdio.h> #include <unistd.h> #include <string.h> void* callBack(void* num) { puts("I'm child pthread"); printf("%d\n", *(int*)num); printf("pthread_id: %ld\n", pthread_self()); // int* val = new int(10); int val = 10; return (void*) 10; } int main() { int num = 10; pthread_t* pid; int ret = pthread_create(pid, NULL, callBack, (void*)&num); if(ret != 0) { puts(strerror(ret)); } int* val; if(ret != 0) { puts(strerror(ret)); } // 需要修改val指针的指向 pthread_join(*pid, (void**) &val); printf("%d\n", *val); }
int pthread_detach(pthread_t thread)
#include <pthread.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <fcntl.h> // callback function void* callback(void* arg) { sleep(1); printf("child thread...\n"); printf("my pid = %d\n", getpid()); printf("arg value: %d\n", *(int *)arg); // 当main return时,如果不带走子线程,那么这个文件会被创建 int fd = open("/home/dyj/byMyself/basic/thread/in.txt", O_CREAT | O_RDWR, 0664); if(fd == -1) { perror("open"); } write(fd, "123", 4); close(fd); } int main() { pthread_t tid; int num = 10; // create a child thread int ret = pthread_create(&tid, NULL, callback, (void *)&num); if(ret != 0) { char* errstr = strerror(ret); printf("error: %s\n", errstr); } pthread_detach(tid); printf("%d\n",(int)getpid()); printf("%ld\n %ld\n", tid, pthread_self()); puts("father thread die"); // sleep(1); pthread_exit(0); return 0; // 有前一句,这句不会被执行 }
int pthread_cancel(pthread_t thread);
puts("cancelled"); #include <stdio.h> #include <unistd.h> #include <string.h> #include <pthread.h> void* callBack(void* arg) { for(int i = 0; i < 5; i++) { printf("I'm pthread, My tid= %ld\n", pthread_self()); } } int main() { pthread_t tid; int ret = pthread_create(&tid, NULL, callBack, NULL); if(ret != 0) { puts(strerror(ret)); _exit(0); } if(ret != 0) { puts(strerror(ret)); _exit(0); } ret = pthread_cancel(tid); ret = pthread_cancel(pthread_self()); puts("cancelled");//有输出,说明不是立即取消 puts("cancelled"); puts("cancelled"); puts("cancelled"); puts("cancelled"); puts("cancelled"); for(int i = 0; i < 5; i++) { printf("I'm man thread, My tid= %ld\n", pthread_self()); } }
体验设置线程的attr属性
#include <stdio.h> #include <pthread.h> #include <string.h> #include <unistd.h> void* callback(void* arg) { puts("Im a pthread"); } int main() { pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_t tid; int ret = pthread_create(&tid, &attr, callback, NULL); if(ret != 0) { puts(strerror(ret)); _exit(0); } size_t size; pthread_attr_getstacksize(&attr, &size); printf("thread stack size: %ld\n", size); printf("tid: %ld, main thread id: %ld\n", tid, pthread_self()); pthread_attr_destroy(&attr); pthread_exit(0); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。