线程概述及操作

1、线程概述

线程——参与系统调度的最小单位

线程是程序最基本的运行单位。

当程序启动时,就有一个进程被OS创建,同时一个线程也立刻运行,该线程叫做程序的主线程。应用程序都是以main()作为入口开始执行的,所以main()函数就是主线程的入口函数,main()函数所执行的任务就是主线程需要执行的任务。

主线程重要性:

1)子线程由主线程创建;

2)主线程通常会在最后结束运行,执行各种清理工作,譬如回收各个子线程。

线程特点:

  • 线程不单独存在,而是包含在进程中;
  • 线程是参与系统调度的基本单位;
  • 可并发执行,同一进程的多个线程之间可并发执行——宏观上同时执行;
  • 共享进程资源。同一进程的各个线程,可共享该进程所拥有的资源(所有线程都有相同的地址空间)——注意,同一进程的多个线程有各自的调用栈(线程栈)。
进程线程
特点

1)进程间切换开销大。多个进程同时运行(指宏观上同时运行,无特别说明,均指宏观上),微观上依然是轮流切换运行,进程间切换开销远大于同一进程的多个线程间切换的开销,通常对于一些中
小型应用程序来说不划算;

2)进程间通信较为麻烦。每个进程都在各自的地址空间中、相互独立、隔离,处在于不同的地址空间中,因此相互通信较为麻烦

⚫ 同一进程的多个线程间切换开销比较小。
⚫  同一进程的多个线程间通信容易。它们共享了进程的地址空间,所以它们都是在同一个地址空间中,通信容易。
⚫  线程创建的速度远大于进程创建的速度。
⚫  多线程在多核处理器上更有优势。

2、创建线程pthread_create

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

static void *new_thread_start(void *arg)
{
	printf("new_thread: ID<%d> pthread_ID<%lu>", getpid(), pthread_self());
	return (void *)0; 
} 


int main(void)
{
	pthread_t tid;
	int ret;
	
	ret = pthread_create(&tid, NULL, new_thread_start, NULL);
	if(ret)
	{
		fprintf(stderr, "Error:%s\n", strerror(ret));
		exit(-1);
	}
	
	printf("main_thread: ID<%d> pthread_ID<%lu>", getpid(), pthread_self());
	
	sleep(1);
	exit(0);
} 

3、终止线程pthread_exit

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

static void *new_thread_start(void *arg)
{
	printf("new_thread start\n");
	sleep(1);
	printf("new_thread end\n");
	pthread_exit(NULL);
} 


int main(void)
{
	pthread_t tid;
	int ret;
	
	ret = pthread_create(&tid, NULL, new_thread_start, NULL);
	if(ret)
	{
		fprintf(stderr, "Error:%s\n", strerror(ret));
		exit(-1);
	}
	
	printf("main_thread end\n");
	pthread_exit(NULL);
	exit(0);
} 

4、回收线程pthread_join

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

static void *new_thread_start(void *arg)
{
	printf("new_thread start\n");
	sleep(2);
	printf("new_thread end\n");
	pthread_exit((void*)10);
} 


int main(void)
{
	pthread_t tid;
	int *tret;
	int ret;
	
	ret = pthread_create(&tid, NULL, new_thread_start, NULL);
	if(ret)
	{
		fprintf(stderr, "pthread_create error:%s\n", strerror(ret));
		exit(-1);
	}
	
	ret = pthread_join(tid, &tret);
	if(ret)
	{
		fprintf(stderr, "pthread_join error:%s\n",strerror(ret));
		exit(-1);
	}
	
	printf("main_thread end,code = %ld\n", (long)tret);

	exit(0);
} 

5、取消线程pthread_cancel

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

static void *new_thread_start(void *arg)
{
	printf("new_thread running\n");
	for(;;)
	{
		sleep(1);
	}
	return (void*)0;
} 


int main(void)
{
	pthread_t tid;
	int *tret;
	int ret;
	
	ret = pthread_create(&tid, NULL, new_thread_start, NULL);
	if(ret)
	{
		fprintf(stderr, "pthread_create error:%s\n", strerror(ret));
		exit(-1);
	}
	
	sleep(1);
	
	/*send cancel request to new thread*/
	ret = pthread_cancel(tid);
	if(ret)
	{
		fprintf(stderr, "pthread_cancel error:%s\n",strerror(ret));
		exit(-1);
	}
	
	/*wait the new thread end*/
	ret = pthread_join(tid, &tret);
	if(ret)
	{
		fprintf(stderr, "pthread_join error:%s\n",strerror(ret));
		exit(-1);
	}
	printf("new_thread end,code = %ld\n", (long)tret);

	exit(0);
} 

通过 pthread_setcancelstate()和 pthread_setcanceltype()来设置线程的取消性
状态和类型。

pthread_setcancelstate

  • PTHREAD_CANCEL_ENABLE :线程可以取消,默认值;
  • PTHREAD_CANCEL_DISABLE :线程不可被取消。

pthread_setcanceltype

  • PTHREAD_CANCEL_DEFERRED :取消请求到来时,线程还是继续运行,取消请求被挂起,直到线程到达某个取消点(cancellation point);
  • PTHREAD_CANCEL_ASYNCHRONOUS :可能会在任何时间点(也许是立即取消,但不一定)取消线程。

6、分离线程pthread_detach

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

static void *new_thread_start(void *arg)
{
	int ret;
	
	/*detach actively*/
	ret = pthread_detach(pthread_self());
	if(ret)
	{
		fprintf(stderr, "pthread_detach error:%s\n", strerror(ret));
		return NULL;
	}
	
	printf("new thred start\n");
	sleep(2);
	printf("new thread end\n");
	pthread_exit(NULL);
} 


int main(void)
{
	pthread_t tid;
	int ret;
	
	ret = pthread_create(&tid, NULL, new_thread_start, NULL);
	if(ret)
	{
		fprintf(stderr, "pthread_create error:%s\n", strerror(ret));
		exit(-1);
	}
	
	sleep(1);
	
	/*wait new thread end*/
	ret = pthread_join(tid, NULL);
	if(ret)
	{
		fprintf(stderr, "pthread_join error:%s\n",strerror(ret));
		exit(-1);
	}
	
	/*wait the new thread end*/
	ret = pthread_join(tid, NULL);
	if(ret)
	{
		fprintf(stderr, "pthread_join error:%s\n",strerror(ret));
		//exit(-1);
	}

	pthread_exit(NULL);
} 

7、注册线程清理函数pthread_cleanup_push

当线程执行以下动作时,清理函数栈中的清理函数才会被执行:
⚫  线程调用 pthread_exit()退出时;
⚫  线程响应取消请求时;
⚫  用非 0 参数调用 pthread_cleanup_pop()

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

static void cleanup(void *arg)
{
	printf("cleanup:%s\n", (char*)arg);
}

static void *new_thread_start(void *arg)
{
	printf("new thread--start run\n");
	pthread_cleanup_push(cleanup, "1st used");
	pthread_cleanup_push(cleanup, "2nd used");
	pthread_cleanup_push(cleanup, "3rd used");
	
	sleep(2);
	pthread_exit((void *)0);
	
	pthread_cleanup_pop(0); 
	pthread_cleanup_pop(0); 
	pthread_cleanup_pop(0); 
} 


int main(void)
{
	pthread_t tid;
	void *tret;
	int ret;
	
	ret = pthread_create(&tid, NULL, new_thread_start, NULL);
	if(ret)
	{
		fprintf(stderr, "pthread_create error:%s\n", strerror(ret));
		exit(-1);
	}
	
	/*wait new thread end*/
	ret = pthread_join(tid, &tret);
	if(ret)
	{
		fprintf(stderr, "pthread_join error:%s\n",strerror(ret));
		exit(-1);
	}
	printf("new thread end, code =%ld\n", (long)tret);
    exit(0);
} 

8、线程属性(初始化、销毁)

设置线程栈大小

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>



static void *new_thread_start(void *arg)
{
	printf("Hello World!\n");
	return (void *)0;
} 


int main(void)
{
	pthread_attr_t attr;
	size_t stacksize;
	pthread_t tid;
	int ret;
	
	/*attr obj initialized*/
	pthread_attr_init(&attr);
	
	/*set the size of stack 4K*/
	pthread_attr_setstacksize(&attr, 4096);
	
	ret = pthread_create(&tid, NULL, new_thread_start, NULL);
	if(ret)
	{
		fprintf(stderr, "pthread_create error:%s\n", strerror(ret));
		exit(-1);
	}
	
	/*wait new thread end*/
	ret = pthread_join(tid, NULL);
	if(ret)
	{
		fprintf(stderr, "pthread_join error:%s\n",strerror(ret));
		exit(-1);
	}
	
	
	pthread_attr_destroy(&attr);
    exit(0);
} 

分离状态启动线程

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>



static void *new_thread_start(void *arg)
{
	printf("Hello World!\n");
	return (void *)0;
} 


int main(void)
{
	pthread_attr_t attr;
	pthread_t tid;
	int ret;
	
	/*attr obj initialized*/
	pthread_attr_init(&attr);
	
	/*set detach state*/
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	
	ret = pthread_create(&tid, NULL, new_thread_start, NULL);
	if(ret)
	{
		fprintf(stderr, "pthread_create error:%s\n", strerror(ret));
		exit(-1);
	}
	
	/*wait new thread end*/
	ret = pthread_join(tid, NULL);
	if(ret)
	{
		fprintf(stderr, "pthread_join error:%s\n",strerror(ret));
		exit(-1);
	}
	
	sleep(1);
	
	pthread_attr_destroy(&attr);
    exit(0);
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值