linux 线程需要注意地方
1.线程创建函数:
int pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
我们知道第一个参数线程ID是传出参数,以便调用它的线程能获取其创建线程的ID,根据线程ID所相应的处理,最后一个参数是传入参数,创建的线程可以获取它做相应操作。
1.我们是否能在创建的线程使用线程ID变量:thread?
当然不能,使用它存在一定的风险,调用它的线程把创建的线程的ID存放在thread中,不过由于thread是一个传出参数,如果新线程在创建线程返回之前就运行了,那么新线程看到thread的值是未初始化的内容,这个内容并不是正确的 线程ID的内容。一般在创建线程中使用pthread_self(),获取当前创建线程的ID。
2 传入参数需要注意的地方,从以下程序来分析:
代码一:
#include <stdio.h> #include <pthread.h> void* start_thread(void *param) { char *str = (char *)param; printf("s:%s\n",__func__, str); return NULL; } pthread_t create_test_thread(char * buf) { pthread_t id =0; pthread_create(&id, NULL, start_thread,buf); return id; } int main(void) { void *ret = NULL; char buf[] ="helloword"; pthread_t id = create_test_thread(buf); return 0; }