【进程、线程和进程间通信】(二)线程编程1

一、线程概念

1.进程和线程区别

  • 进程
    进程有独立的地址空间
    Linux为每个进程创建task_struct
    每个进程都参与内核调度,互不影响

  • 线程
    进程在切换时系统开销大
    很多操作系统引入了轻量级进程LWP
    同一进程中的线程共享相同地址空间
    Linux不区分进程、线程

2.线程特点

  • 通常线程指的是共享相同地址空间的多个任务

  • 使用多线程的好处:
    大大提高了任务切换的效率,避免了额外的TLB & cache的刷新

  • 一个进程中的多个线程共享以下资源:
    可执行的指令
    静态数据
    进程中打开的文件描述符
    当前工作目录
    用户ID
    用户组ID

  • 每个线程私有的资源包括:
    线程ID (TID)
    PC(程序计数器)和相关寄存器
    堆栈
    错误号 (errno)
    优先级
    执行状态和属性

二、线程相关函数

1.linux线程库

注意:在编译多线程程序时,要确保有编译好pthread的静态库

示例:gcc -o test test.c -lpthread

  • pthread线程库中提供了如下基本操作
    创建线程
    回收线程
    结束线程

  • 同步和互斥机制
    信号量
    互斥锁

2.线程的创建和回收

(1)线程创建:pthread_create

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

成功返回0,失败时返回错误码
thread 线程对象
attr 线程属性,NULL代表默认属性
routine 线程执行的函数
arg 传递给routine的参数 ,参数是void * ,注意传递参数格式

(2)线程结束:pthread_exit

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

结束当前线程
retval可被其他线程通过pthread_join获取
线程私有资源被释放

(3)线程查看tid函数:pthread_self

pthread_t pthread_self(void) 查看自己的TID
#include <pthread.h>
pthread_t pthread_self(void);

(4)线程回收:pthread_join

#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);

对于一个默认属性的线程 A 来说,线程占用的资源并不会因为执行结束而得到释放
成功返回0,失败时返回错误码
thread 要回收的线程对象
调用线程阻塞直到thread结束
*retval 接收线程thread的返回值

(5)线程分离:pthead_detach

两种方式:

  1. 使用pthread_detach
    int pthread_detach(pthread_t thread);
    成功:0;失败:错误号
    指定该状态,线程主动与主控线程断开关系。线程结束后(不会产生僵尸线程)

  2. 创建线程时候设置为分离属性
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

(6)线程回收和线程分离区别

链接: link
当我们使用默认属性创建一个线程的时候,线程是joinable的,对于一个joinable的线程, 线程创建者要调用pthread_join回收资源,否则可能会存在存储器资源泄漏,而对于datached的线程的存储器资源在它终止时由系统自动释放。

某项目中,用默认方式创建的线程,然后创建线程的函数在返回前忘记调用pthread_join函数,并且创建线程的函数是反复被调用的,这样每调用一次都会创建一个线程,并且没有回收线程资源,测试时发现存在内存泄漏,最后定位为线程资源没有释放。

(7)线程查看命令:ps -eLf

  • 查看某个进程的线程
    ps -eLf|grep xxx

  • 查看进程占用资源
    ps -ef|grep xxx(先查看PID)
    top -p PID

3.线程的取消和互斥

(1)线程取消:pthread_cancel

  • 线程的取消
    意义:随时杀掉一个线程
    int pthread_cancel(pthread_t thread);
    注意:线程的取消要有取消点才可以,不是说取消就取消,线程的取消点主要是阻塞的系统调用

  • 如果没有取消点,手动设置一个
    void pthread_testcancel(void);

  • 设置取消使能或禁止
    int pthread_setcancelstate(int state, int *oldstate);
    PTHREAD_CANCEL_ENABLE
    PTHREAD_CANCEL_DISABLE

  • 设置取消类型
    int pthread_setcanceltype(int type, int *oldtype);
    PTHREAD_CANCEL_DEFERRED 等到取消点才取消
    PTHREAD_CANCEL_ASYNCHRONOUS 目标线程会立即取消

(2)线程清理:pthread_cleanup_push、pthread_cleanup_pop

必要性: 当线程非正常终止,需要清理一些资源。

void pthread_cleanup_push(void (*routine) (void *), void *arg)
void pthread_cleanup_pop(int execute)

routine 函数被执行的条件:

  1. 被pthread_cancel取消掉。
  2. 执行pthread_exit
  3. 非0参数执行pthread_cleanup_pop()

注意:

  1. 必须成对使用,即使pthread_cleanup_pop不会被执行到也必须写上,否则编译错误。
  2. pthread_cleanup_pop()被执行且参数为0,pthread_cleanup_push回调函数routine不会被执行。
  3. pthread_cleanup_push 和pthread_cleanup_pop可以写多对,routine执行顺序正好相反。
  4. 线程内的return可以结束线程,也可以给pthread_join返回值,但不能触发pthread_cleanup_push里面的回调函数,所以我们结束线程尽量使用pthread_exit退出线程。

(3)线程的互斥和同步

临界资源概念:
一次只允许一个任务(进程、线程)访问的共享资源。
不能同时访问的资源,比如写文件,只能由一个线程写,同时写会写乱。
比如外设打印机,打印的时候只能由一个程序使用。
外设基本上都是不能共享的资源。
生活中比如卫生间,同一时间只能由一个人使用。

临界区:
访问临界资源的代码

互斥机制:
mutex互斥锁
任务访问临界资源前申请锁,访问完后释放锁

必要性: 临界资源不可以共享

man手册找不到 pthread_mutex_xxxxxxx (提示No manual entry for pthread_mutex_xxx)的解决方法:
apt-get install manpages-posix-dev

(4)互斥锁的创建和销毁:pthread_mutex_lock、pthread_mutex_unlock

两种方法创建互斥锁,静态方式和动态方式
动态方式:
int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);
其中mutexattr用于指定互斥锁属性,如果为NULL则使用缺省属性。
静态方式:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

锁的销毁:
int pthread_mutex_destroy(pthread_mutex_t *mutex)
在Linux中,互斥锁并不占用任何资源,因此LinuxThreads中的 pthread_mutex_destroy()除了检查锁状态以外(锁定状态则返回EBUSY)没有其他动作。

互斥锁的使用:
int pthread_mutex_lock(pthread_mutex_t *mutex) //阻塞状态
int pthread_mutex_unlock(pthread_mutex_t *mutex)
int pthread_mutex_trylock(pthread_mutex_t *mutex) //当请求的锁正在被占用的时候, 不会进入阻塞状态,而是立刻返回,并返回一 个错误代码 EBUSY

vim 设置代码全文格式化:gg=G

(5)读写锁:pthread_rwlock_rdlock、pthread_rwlock_wrlock

必要性:提高线程执行效率

特性:
写者:写者使用写锁,如果当前没有读者,也没有其他写者,写者立即获得写锁;否则写者将等待,直到没有读者和写者。
读者:读者使用读锁,如果当前没有写者,读者立即获得读锁;否则读者等待,直到没有写者。

注意:
同一时刻只有一个线程可以获得写锁,同一时刻可以有多个线程获得读锁。
读写锁出于写锁状态时,所有试图对读写锁加锁的线程,不管是读者试图加读锁,还是写者试图加写锁,都会被阻塞。
读写锁处于读锁状态时,有写者试图加写锁时,之后的其他线程的读锁请求会被阻塞,以避免写者长时间的不写锁

初始化一个读写锁 pthread_rwlock_init
读锁定读写锁 pthread_rwlock_rdlock
非阻塞读锁定 pthread_rwlock_tryrdlock
写锁定读写锁 pthread_rwlock_wrlock
非阻塞写锁定 pthread_rwlock_trywrlock
解锁读写锁 pthread_rwlock_unlock
释放读写锁 pthread_rwlock_destroy

(6)死锁的避免

概念:
在这里插入图片描述

避免方法:

  1. 锁越少越好,最好使用一把锁
  2. 调整好锁的顺序(或者调整获取锁时间差)

三、示例代码

1.线程的创建和回收

(1)线程创建(两种情况)

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

/*
int *testPthread(char *arg)
{
	printf("this is test pthread\n");
	pthread_exit(0);
}
*/

void *testPthread(void *arg)
{
	printf("this is test pthread\n");
	pthread_exit(0);  //结束当前线程,线程私有资源被释放
	printf("test pthread end\n");
}

int main(int argc, char **argv)
{
	pthread_t tid;
	int ret;

	//ret = pthread_create(&tid, NULL, (void *)testPthread, NULL);
	ret = pthread_create(&tid, NULL, testPthread, NULL);
	printf("this is main pthread\n");
	sleep(1);
	
	return 0;
}

(2)查看线程pid、tid

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *testPthread(void *arg)
{
	printf("this is test pthread, pid = %d, tid = %lu\n", getpid(), pthread_self());
	pthread_exit(0);
}

int main(int argc, char **argv)
{
	pthread_t tid;
	int ret;

	ret = pthread_create(&tid, NULL, testPthread, NULL);
	printf("this is main pthread, pid = %d, tid = %lu\n", getpid(), tid);
	sleep(1);
	
	return 0;
}

(3)线程间参数传递(地址传递)

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *testPthread(void *arg)
{
	printf("this is test pthread, pid = %d, tid = %lu\n", getpid(), pthread_self());
	printf("value = %d\n", *(int *)arg);
	pthread_exit(0);
}

int main(int argc, char **argv)
{
	pthread_t tid;
	int ret;
	int value = 5;
	
	ret = pthread_create(&tid, NULL, testPthread, (void *)&value);
	printf("this is main pthread, pid = %d, tid = %lu\n", getpid(), tid);
	sleep(1);
	
	return 0;
}

(4)线程间参数传递(值传递)

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *testPthread(void *arg)
{
	printf("this is test pthread, pid = %d, tid = %lu\n", getpid(), pthread_self());
	printf("value = %d\n", (int)arg);
	pthread_exit(0);
}

int main(int argc, char **argv)
{
	pthread_t tid;
	int ret;
	int value = 5;

	ret = pthread_create(&tid, NULL, testPthread, (void *)value);
	printf("this is main pthread, pid = %d, tid = %lu\n", getpid(), tid);
	sleep(1);
	
	return 0;
}

(5)创建多个线程(值传递)

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
	
void *testPthread(void *arg)
{
	printf("this is test pthread, pid = %d, tid = %lu\n", getpid(), pthread_self());
	printf("arg = %d\n", (int)arg);
	pthread_exit(0);
}

int main(int argc, char **argv)
{
	pthread_t tid[5];
	int ret;
	int i;

	for (i = 0; i < 5; i++) {
		ret = pthread_create(&tid[i], NULL, testPthread, (void *)i);
		//sleep(1);  //值传递不需要这里sleep
		printf("this is main pthread, pid = %d, tid = %lu\n", getpid(), tid[i]);
	}

	sleep(1);
	
	return 0;
}

(6)线程分离(方式1)

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *testPthread(void *arg)
{
	pthread_detach(pthread_self());  //pthread_detach(tid[i]);
	printf("this test Pthread\n");
	sleep(25);
	pthread_exit(NULL);
}

int main(int argc, char **argv)
{
	pthread_t tid[50];	
	int i;

	for (i = 0; i < 50; i++) {	
		pthread_create(&tid[i], NULL, testPthread, NULL);
		//pthread_detach(tid[i]);
	}

	while (1) {
		sleep(1);
	}
	
	return 0;
}

(7)线程分离(方式2)

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *testPthread(void *arg)
{
	printf("this test Pthread\n");
	sleep(25);
	pthread_exit(NULL);
}

int main(int argc, char **argv)
{
	pthread_t tid[50];
	int i;	

	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

	for (i = 0; i < 50; i++) {	
		pthread_create(&tid[i], &attr, testPthread, NULL);
	}

	while (1) {
		sleep(1);
	}
	
	return 0;
}

(8)线程回收

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *testPthread(void *arg)
{	
	printf("this test Pthread\n");
	sleep(10);
	pthread_exit("this test pthread return");
}

int main(int argc, char **argv)
{
	pthread_t tid;	
	void *retv;
	
	pthread_create(&tid, NULL, testPthread, NULL);
	pthread_join(tid, &retv);  //pthread_join 是阻塞函数,如果回收的线程没有结束,则一直等待
	printf("receive: %s\n", (char *)retv);
	
	while (1) {
		sleep(1);
	}
	
	return 0;
}

(9)回收多个线程

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *testPthread(void *arg)
{	
	printf("this test Pthread\n");
	sleep(25);
	pthread_exit("this test pthread return");
}

int main(int argc, char **argv)
{
	pthread_t tid[50];	
	void *retv;
	int i;

	for (i = 0; i < 50; i++) {	
		pthread_create(&tid[i], NULL, testPthread, NULL);
	}

	for (i = 0; i< 50; i++) {	
		pthread_join(tid[i], &retv);  //pthread_join 是阻塞函数,如果回收的线程没有结束,则一直等待
		printf("receive: %s\n", (char *)retv);
	}

	while (1) {
		sleep(1);
	}
	
	return 0;
}

2.线程的取消和互斥

(1)线程取消

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *testPthread(void *arg)
{
	printf("this is testPthread\n");
	
	while (1) {
		pthread_testcancel();
		//sleep(1);
	}

	pthread_exit("test pthread end");
}

int main(int argc, char **argv)
{
	pthread_t tid;
	void *retv;
	
	pthread_create(&tid, NULL, testPthread, NULL);
	sleep(5);
	pthread_cancel(tid);  //线程的取消要有取消点才可以,不是说取消就取消,线程的取消点主要是阻塞的系统调用
	pthread_join(tid, &retv);  
	//printf("receive: %s\n", (char *)retv);

	while (1) {
		sleep(1);
	}	
	
	return 0;
}

(2)线程取消(使能或禁止取消点)

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *testPthread(void *arg)
{
	printf("this is testPthread\n");
	
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);  //设置取消点无用(默认取消点有用)
	//while (1)
	{
		pthread_testcancel();
		sleep(10);
	}
	
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);  //设置取消点有用

	while (1) {
		sleep(1);
	}

	pthread_exit("test pthread end");
}

int main(int argc, char **argv)
{
	pthread_t tid;
	void *retv;
	
	pthread_create(&tid, NULL, testPthread, NULL);
	sleep(1);  //setcancelstate需要一点时间
	pthread_cancel(tid);
	pthread_join(tid, &retv);
	//printf("receive: %s\n", (char *)retv);

	while (1) {
		sleep(1);
	}	
	
	return 0;
}

(3)线程清理

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void routine1(void *arg)
{
	printf("cleanup1,arg=%s\n", (char *)arg);
}

void routine2(void *arg)
{
	printf("cleanup2,arg=%s\n", (char *)arg);
}

void *testPthread(void *arg)
{
	printf("this is testPthread\n");
	
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);  //目标线程会立即取消
	
	pthread_cleanup_push(routine1, "routine1")
	pthread_cleanup_push(routine2, "routine2");

	//while (1)
	{
		sleep(1);
	}

	pthread_cancel(pthread_self());  //routine函数情况1
	printf("after cancel, should not print\n");
	while (1) {
		printf("sleep\n");
		sleep(1);  //设置取消点
	}

	pthread_cleanup_pop(1);   //routine函数执行情况2
	pthread_cleanup_pop(1);
	pthread_exit("test pthread end");  //routine函数情况3
}

int main(int argc, char **argv)
{
	pthread_t tid;
	void *retv;
	
	pthread_create(&tid, NULL, testPthread, NULL);
	sleep(1);  
	pthread_join(tid, &retv);
//	printf("receive: %s\n", (char *)retv);

	while (1) {
		sleep(1);
	}	
	
	return 0;
}

(4)互斥锁的使用(两线程写一个文件时)

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>

FILE *fp;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void *fun1(void *arg)
{
	pthread_detach(pthread_self());
	printf("this is fun1\n");

	char str[] = "hello world fun1\n";
	int i = 0;
	char c;

	while (1)
	{
		pthread_mutex_lock(&mutex);
		while (i < strlen(str)) {
			c = str[i++];	
			fputc(c, fp);
			usleep(1);
		}
		pthread_mutex_unlock(&mutex);

		i = 0;
		usleep(1);
	}	

	pthread_exit("fun1 exit\n");
}

void *fun2(void *arg)
{
	pthread_detach(pthread_self());
	printf("this is fun2\n");
	
	char str[] = "hello world fun2\n";
	int i = 0;
	char c;

	while (1)
	{
		pthread_mutex_lock(&mutex);
		while (i < strlen(str)) {
			c = str[i++];	
			fputc(c, fp);
			usleep(1);
		}
		pthread_mutex_unlock(&mutex);
	
		i = 0;
		usleep(1);
	}	

	pthread_exit("fun2 exit\n");
}

int main(int argc, char **argv)
{
	pthread_t tid1,tid2;
	
	fp = fopen("1.txt", "a+");
		
	pthread_create(&tid1, NULL, fun1, NULL);
	pthread_create(&tid2, NULL, fun2, NULL);
		
	while (1) {
		sleep(1);
	}

	return 0;
}

(5)读写锁的使用(两线程读写一个文件时)

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>

FILE *fp;
pthread_rwlock_t rwmutex;

void *read_fun(void *arg)
{
	pthread_detach(pthread_self());
	printf("this is read fun\n");

	char buf[32] = {0};
	while (1) {
		rewind(fp);
		pthread_rwlock_rdlock(&rwmutex);  //读锁,两个线程可一起读
//		pthread_rwlock_wrlock(&rwmutex);  //写锁,只有一个线程可以读
		while (fgets(buf, 32, fp) != NULL) {
			printf("arg:%d,read:%s\n", (int)arg, buf);
			usleep(1000);
		}
		pthread_rwlock_rdlock(&rwmutex);
		sleep(1);		
	}
}

void *fun1(void *arg)
{
	pthread_detach(pthread_self());
	printf("this is write fun1\n");

	char str[] = "hello world fun1\n";
	int i = 0;
	char c;

	while (1)
	{
		pthread_rwlock_wrlock(&rwmutex);
		while (i < strlen(str)) {
			c = str[i++];	
			fputc(c, fp);
			usleep(1);
		}
		pthread_rwlock_wrlock(&rwmutex);

		i = 0;
		usleep(1);
	}	

	pthread_exit("fun1 exit\n");
}

void *fun2(void *arg)
{
	pthread_detach(pthread_self());
	printf("this is write fun2\n");

	char str[] = "hello world fun2\n";
	int i = 0;
	char c;

	while (1)
	{
		pthread_rwlock_wrlock(&rwmutex);
		while (i < strlen(str)) {
			c = str[i++];	
			fputc(c, fp);
			usleep(1);
		}
		pthread_rwlock_wrlock(&rwmutex);
	
		i = 0;
		usleep(1);
	}	

	pthread_exit("fun2 exit\n");
}

int main(int argc, char **argv)
{
	pthread_t tid1,tid2,tid3,tid4;	
  	fp = fopen("1.txt", "a+");
	if (fp == NULL) {
		perror("fopen");
		return -1;
	}

	pthread_rwlock_init(&rwmutex, NULL);
	pthread_create(&tid1, NULL, read_fun, 1);
	pthread_create(&tid2, NULL, read_fun, 2);
//	sleep(1);  //使用写锁时测试用
	pthread_create(&tid3, NULL, fun1, NULL);
	pthread_create(&tid4, NULL, fun2, NULL);
		
	while (1) {
		sleep(1);
	}

	return 0;
}

(6)死锁

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;

void *fun1(void *arg)
{
	pthread_detach(pthread_self());
	printf("this is fun1\n");

	while (1)
	{
		pthread_mutex_lock(&mutex);
		printf("fun%d got lock1\n", (int)arg);
		sleep(1);
		pthread_mutex_lock(&mutex2);
		printf("fun%d got 2 locks\n", (int)arg);

		pthread_mutex_unlock(&mutex2);
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}	

	pthread_exit("fun1 exit\n");
}

void *fun2(void *arg)
{
	pthread_detach(pthread_self());
	printf("this is fun2\n");

	while (1)
	{
		pthread_mutex_lock(&mutex2);
		printf("fun%d got lock2\n", (int)arg);
		sleep(1);
		pthread_mutex_lock(&mutex);
		printf("fun%d got 2 locks\n", (int)arg);

		pthread_mutex_unlock(&mutex);
		pthread_mutex_unlock(&mutex2);
		sleep(1);
	}	

	pthread_exit("fun2 exit\n");
}

int main(int argc, char **argv)
{
	pthread_t tid1,tid2;	
		
	pthread_create(&tid1, NULL, fun1, 1);
	pthread_create(&tid2, NULL, fun2, 2);
		
	while (1) {
		sleep(1);
	}

	return 0;
}

(7)避免死锁(调整锁的顺序)

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;

void *fun1(void *arg)
{
	pthread_detach(pthread_self());
	printf("this is fun1\n");

	while (1)
	{
		pthread_mutex_lock(&mutex);
		printf("fun%d got lock1\n", (int)arg);
		sleep(1);
		pthread_mutex_lock(&mutex2);
		printf("fun%d got 2 locks\n", (int)arg);

		pthread_mutex_unlock(&mutex2);
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}	

	pthread_exit("fun1 exit\n");
}

void *fun2(void *arg)
{
	pthread_detach(pthread_self());
	printf("this is fun2\n");

	while (1)
	{
		pthread_mutex_lock(&mutex);  //调整锁的顺序
		printf("fun%d got lock2\n", (int)arg);
		sleep(1);
		pthread_mutex_lock(&mutex2);
		printf("fun%d got 2 locks\n", (int)arg);

		pthread_mutex_unlock(&mutex2);
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}	

	pthread_exit("fun2 exit\n");
}

int main(int argc, char **argv)
{
	pthread_t tid1,tid2;	
		
	pthread_create(&tid1, NULL, fun1, 1);
	pthread_create(&tid2, NULL, fun2, 2);
		
	while (1) {
		sleep(1);
	}

	return 0;
}

四、编译错误分析

1.注意事项

  1. 主进程的退出,它创建的线程也会退出。
  2. 线程创建需要时间,如果主进程马上退出,那线程不能得到执行
  3. 获取线程的id
    通过pthread_create函数的第一个参数;
    通过在线程里面调用pthread_self函数。

2.线程创建错误

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);

3.链接错误

createP_t.c:(.text+0x4b):对‘pthread_create’未定义的引用
collect2: error: ld returned 1 exit status --------这个链接错误,
表示pthread_create这个函数没有实现
解决方法:编译时候加 -lpthread

4.参数传递错误

编译错误:
createP_t.c:8:34: warning: dereferencing ‘void *’ pointer
printf(“input arg=%d\n”,(int)*arg);
^
createP_t.c:8:5: error: invalid use of void expression
printf(“input arg=%d\n”,(int)arg);
错误原因是void 类型指针不能直接用取值(arg),因为编译不知道数据类型。
解决方法:转换为指定的指针类型后再用
取值 比如:
(int *)arg

  1. 通过地址传递参数,注意类型的转换
  2. 值传递,这时候编译器会告警,需要程序员自己保证数据长度正确

5.运行错误,调试(核心已转储)

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

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

  • 运行段错误调试:
    可以使用gdb调试
    使用gdb 运行代码,gdb ./youapp
    (gdb) run
    等待出现Thread 1 “pcancel” received signal SIGSEGV, Segmentation fault.
    输入命令bt(打印调用栈)
    (gdb) bt
    #0 0x00007ffff783ecd0 in vfprintf () from /lib/x86_64-linux-gnu/libc.so.6
    #1 0x00007ffff78458a9 in printf () from /lib/x86_64-linux-gnu/libc.so.6
    #2 0x00000000004007f9 in main () at pcancel.c:21
    确定段错误位置是pcancel.c 21行
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值