在使用線程之前我們要知道線程是個(gè)怎樣的概念,它的作用是什么?
在學(xué)多進(jìn)程或多線程編程的時(shí)候,有這樣一個(gè)概念----進(jìn)程是資源管理的小單位,線程是程序執(zhí)行的小單位,相對(duì)于進(jìn)程,線程上下文切換花費(fèi)的開銷更少。因?yàn)榫程共享了內(nèi)存空間,不同線程可以訪問同一個(gè)變量,即多有線程都可以訪問已經(jīng)聲明了的全局變量。在多進(jìn)程中這種情況需要使用IPC(進(jìn)程間通信)對(duì)象實(shí)現(xiàn),這樣增加了額外的開銷,從而降低了性能,并且增加了代碼的復(fù)雜度。
在執(zhí)行方面線程也有著它的優(yōu)勢(shì),因?yàn)樵趧?chuàng)建線程的過程中系統(tǒng)無需復(fù)制內(nèi)存空間和文件描述符等,這樣節(jié)省了很多的cpu時(shí)間。
首先我們了解幾個(gè)相關(guān)函數(shù)
pthread_create()創(chuàng)建線程,函數(shù)原型為:
int pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
thread 返回創(chuàng)建線程的ID,attr是創(chuàng)建線程是設(shè)置的線程屬性。start_routine是線程執(zhí)行體函數(shù),arg為傳遞到線程執(zhí)行體函數(shù)的一個(gè)參數(shù)。
下面我們寫一個(gè)簡單的多線程的程序。
pthread1.c
#include <stdio.h>
#include <pthread.h>
void *thread_a(void *arg)
{
printf("thread a enter\n");
}
void *thread_b(void *arg)
{
printf("thread b enter\n");
}
int main(int argc, char **argv)
{
pthread_t tid_a,tid_b;
int err;
err = pthread_create(&tid_a,NULL,thread_a,NULL);
if(err < 0)
{
perror("pthread_create thread_a");
}
err = pthread_create(&tid_b,NULL,thread_b,NULL);
if(err < 0)
{
perror("pthread_create thread_a");
}
sleep(5);
printf("the main close\n");
return 0;
}
在這個(gè)程序中我們創(chuàng)建了兩個(gè)線程分別是tid_a和tid_b,thread_a和thread_b分別是他們的執(zhí)行體。這個(gè)時(shí)候我們的程序中有了三個(gè)線程。因?yàn)槌宋覀兂R姷膬蓚(gè)線程外,主程序也是一個(gè)線程。下面我們嘗試一下傳遞一個(gè)參數(shù)到線程執(zhí)行體中。
pthread2.c
#include <stdio.h>
#include <pthread.h>
void *thread_a(void *arg)
{
printf("thread a = %d enter\n",*(int *)arg);
}
void *thread_b(void *arg)
{
printf("thread b = %d enter\n", *(int *)arg);
}
int main(int argc, char **argv)
{
pthread_t tid_a,tid_b;
int err;
err = pthread_create(&tid_a,NULL,thread_a,(void *)&tid_a);
if(err < 0)
{
perror("pthread_create thread_a");
}
printf("create tid_a = %d\n",tid_a);
err = pthread_create(&tid_b,NULL,thread_b,(void *)&tid_b);
if(err < 0)
{
perror("pthread_create thread_a");
}
printf("create tid_b = %d\n",tid_b);
sleep(5);
printf("the main close\n");
return 0;
}