> 文章列表 > day5 线程的创建和参数传递

day5 线程的创建和参数传递

day5 线程的创建和参数传递

线程的基本特点

什么是进程

1、进程有独立的地址空间;

2、Linux为每个进程创建task_struct;

3、每个进程都参与内核的调度,互不影响;

什么是线程:

1、进程在切换是系统开销大;

2、很多操作系统引入了轻量级进程LWP;

3、同一进程中的线程共享相同地址空间;

4、Linux不区分进程、线程;

线程的特点:

1、通常线程指的是共享地址空间的多个任务;

2、使用多线程的好处

        大大提高了任务切换的效率;

        避免了额外的TLB &cache的刷新

线程共享资源 线程私有资源
可执行的指令 线程ID(TID)
静态数据 PC(程序计数器)和相关寄存器
进程中打开的文件描述符 堆栈
用户ID 错误号(error)
用户组ID 优先级、执行状态和属性

线程的创建与结束

线程创建函数

#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*routine)(void *), void *arg);

成功时返回0,失败时返回错误码;

参数:

thread:线程对象;

pthread_attr_t *attr :是一个结构体,传入线程的属性;

void *(*routine)(void *):传入线程的回调函数,返回值和传入参数都是void *;

attr:线程属性,NULL代表默认属性;

routine:线程执行的函数;

arg:传递给routine的参数,参数是void * ,注意传递参数格式;

代码实现:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>int *testThread(char *arg) {printf("This is a thread test\\n");return NULL;
}int main()
{pthread_t tid;int ret;ret = pthread_create(&tid, NULL, (void*)testThread, NULL);printf("this is main thread\\n");sleep(1);}

注意:主进程的退出,它创建的线程也会退出。线程创建需要时间,如果主进程马上退出,那线程不能得到执行;

获取线程ID

1、 pthread_self(void)函数

#include <pthread.h>
pthread_t pthread_self(void)

功能:查看自己的TID;

2、直接在主进程在打印

printf("TID=%lu\\n", tid)

线程结束函数

#include <pthread.h>
void pthread_exit(void *retval);

结束当前线程;

retval可被其他线程通过pthread_join获取;

线程私有资源被释放;

代码实现:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>int *testThread(char *arg) {printf("This is a thread test tid=%lu\\n", pthread_self());pthread_exit(NULL);
}int main()
{pthread_t tid;int ret;ret = pthread_create(&tid, NULL, (void*)testThread, NULL);printf("this is main thread tid=%lu\\n", tid);sleep(1);}

 

编译错误分析

错误1

createP_t.c:14:36: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]ret = pthread_create(&tid,NULL,testThread,NULL);^
In file included from createP_t.c:1:0:
/usr/include/pthread.h:233:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘int * (*)(char *)’

意义:表示pthread_create参数3的定义和实际代码不符合,期望的是void * (*)(void *) ,实际的代码是int * (*)(char *)

解决方法:改为pthread_create(&tid,NULL,(void*)testThread,NULL);

错误2,链接错误

createP_t.c:(.text+0x4b):对‘pthread_create’未定义的引用
collect2: error: ld returned 1 exit status

意义:这个链接错误表示pthread_create这个函数没有实现

解决方法:编译时候加 -lpthread

线程间参数传递

方式1:通过地址传递参数,注意类型的转换;

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>void *testThread(void *arg) {printf("This is a thread test tid=%lu\\n", pthread_self());printf("intput arg=%d\\n", *(int *)arg);pthread_exit(NULL);
}int main()
{pthread_t tid;int ret;int arg = 5;ret = pthread_create(&tid, NULL, (void*)testThread, (void*)&arg);printf("this is main thread tid=%lu\\n", tid);sleep(1);}

编译错误之处:

createP_t.c:8:34: warning: dereferencing ‘void *’ pointerprintf("input arg=%d\\n",(int)*arg);^
createP_t.c:8:5: error: invalid use of void expressionprintf("input arg=%d\\n",(int)*arg);

错误原因:是void *类型指针不能直接用*取值(*arg),因为编译不知道数据类型。

解决方法:转换为指定的指针类型后再用*取值  比如:*(int *)arg

2、值传递,这时候编译器会告警,需要程序员自己保证数据长度正确

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>void *testThread(void *arg) {printf("This is a thread test tid=%lu\\n", pthread_self());printf("intput arg=%d\\n", (int)arg);pthread_exit(NULL);
}int main()
{pthread_t tid;int ret;int arg = 5;ret = pthread_create(&tid, NULL, (void*)testThread, (void *)arg);printf("this is main thread tid=%lu\\n", tid);sleep(1);}

问题:使用pthread_create实现 10 个子线程,并且让每个子线程打印自己的线程号

代码实现:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>void *testThread(void *arg) {printf("This is a thread test tid=%lu\\n", pthread_self());printf("this is %d thread\\n",(int)arg);pthread_exit(NULL);
}int main()
{pthread_t tid[10];int ret;int arg;int i;for (i = 0; i < 10; i++) {ret = pthread_create(&tid[i], NULL, testThread, (void *)i);printf("this is main thread tid=%lu\\n", tid[i]);}sleep(1);}

运行遇到错误:

stack smashing detected ***: ./mthread_t terminated已放弃 (核心已转储)

原因:栈被破坏了(数组越界)

帝国cms技术