在使用線程之前我們要知道線程是個怎樣的概念,它的作用是什么?
在學多進程或多線程編程的時候,有這樣一個概念----進程是資源管理的小單位,線程是程序執(zhí)行的小單位,相對于進程,線程上下文切換花費的開銷更少。因為線程共享了內存空間,不同線程可以訪問同一個變量,即多有線程都可以訪問已經聲明了的全局變量。在多進程中這種情況需要使用IPC(進程間通信)對象實現,這樣增加了額外的開銷,從而降低了性能,并且增加了代碼的復雜度。
在執(zhí)行方面線程也有著它的優(yōu)勢,因為在創(chuàng)建線程的過程中系統(tǒng)無需復制內存空間和文件描述符等,這樣節(jié)省了很多的cpu時間。
首先我們了解幾個相關函數
pthread_create()創(chuàng)建線程,函數原型為:
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)建線程是設置的線程屬性。start_routine是線程執(zhí)行體函數,arg為傳遞到線程執(zhí)行體函數的一個參數。
下面我們寫一個簡單的多線程的程序。
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;
}
在這個程序中我們創(chuàng)建了兩個線程分別是tid_a和tid_b,thread_a和thread_b分別是他們的執(zhí)行體。這個時候我們的程序中有了三個線程。因為除了我們常見的兩個線程外,主程序也是一個線程。下面我們嘗試一下傳遞一個參數到線程執(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;
}