线程——参与系统调度的最小单位。
线程是程序最基本的运行单位。
当程序启动时,就有一个进程被OS创建,同时一个线程也立刻运行,该线程叫做程序的主线程。应用程序都是以main()作为入口开始执行的,所以main()函数就是主线程的入口函数,main()函数所执行的任务就是主线程需要执行的任务。
主线程重要性:
1)子线程由主线程创建;
2)主线程通常会在最后结束运行,执行各种清理工作,譬如回收各个子线程。
线程特点:
进程 | 线程 | |
特点 | 1)进程间切换开销大。多个进程同时运行(指宏观上同时运行,无特别说明,均指宏观上),微观上依然是轮流切换运行,进程间切换开销远大于同一进程的多个线程间切换的开销,通常对于一些中 2)进程间通信较为麻烦。每个进程都在各自的地址空间中、相互独立、隔离,处在于不同的地址空间中,因此相互通信较为麻烦 |
⚫ 同一进程的多个线程间切换开销比较小。 ⚫ 同一进程的多个线程间通信容易。它们共享了进程的地址空间,所以它们都是在同一个地址空间中,通信容易。 ⚫ 线程创建的速度远大于进程创建的速度。 ⚫ 多线程在多核处理器上更有优势。 |
#include
#include
#include
#include
#include
#include
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);
}
#include
#include
#include
#include
#include
#include
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);
}
#include
#include
#include
#include
#include
#include
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);
}
#include
#include
#include
#include
#include
#include
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
#include
#include
#include
#include
#include
#include
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);
}
当线程执行以下动作时,清理函数栈中的清理函数才会被执行:
⚫ 线程调用 pthread_exit()退出时;
⚫ 线程响应取消请求时;
⚫ 用非 0 参数调用 pthread_cleanup_pop()
#include
#include
#include
#include
#include
#include
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);
}
设置线程栈大小
#include
#include
#include
#include
#include
#include
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
#include
#include
#include
#include
#include
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);
}