線程解析(四)
作者:曹忠明,華清遠見嵌入式學(xué)院講師。
一、線程控制
上一節(jié)我們講了使用互斥量實現(xiàn)線程的同步,這里我們介紹一下另外一種常用的方法,POSIX提供的無名信號量sem,PV原語是對整數(shù)計數(shù)器信號量sem的操作,P操作判斷sem資源數(shù)是否為0,不為0則進行P操作,一次P操作可使sem減一,而一次V操作可使sem加一。下面是POSIX提供的一些接口函數(shù):
1、信號量初始化
#include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned int value);
函數(shù)參數(shù):
sem:信號量
pshared:一個參數(shù)一般為0,表示同一個進程中的線程共享一個信號量。
value:信號量資源個數(shù)
2、其余函數(shù)
int sem_wait (sem_t* sem);
int sem_trywait (sem_t* sem);
int sem_post (sem_t* sem);
int sem_getvalue (sem_t* sem);
int sem_destroy (sem_t* sem);
sem_wait和sem_trywait相當(dāng)于P操作,它們都能將信號量的值減一,兩者的區(qū)別在于若信號量的值小于零時,sem_wait將會阻塞進程,而sem_trywait則會立即返回。
sem_post相當(dāng)于V操作,它將信號量的值加一,同時發(fā)出喚醒的信號給等待的進程(或線程)。
sem_getvalue 得到信號量的值。
sem_destroy 摧毀信號量。
下面用一個例程說明信號量的使用:
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <semaphore.h>
sem_t sem;
pid_t gettid(void)
{
return syscall(SYS_gettid);
}
void *thread_a(void *arg)
{
printf("thread a entern");
sem_wait(&sem); //sem - 1
printf("thread a P operationn");
sleep(10);
sem_post(&sem); //sem + 1
printf("thread a V operationn");
}
void *thread_b(void *arg)
{
printf("thread b entern");
sem_wait(&sem); //sem - 1
printf("thread b P operationn");
sleep(10);
sem_post(&sem); //sem + 1
printf("thread b V operationn");
}
int main(int argc, char **argv)
{
pthread_t tid_a,tid_b;
int err;
sem_init(&sem, 0, 1);
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(30);
sem_destroy(&sem);
printf("the main closen");
return 0;
}
二、POSIX tid和linux tid
前面我們說創(chuàng)建線程的時候提到一個函數(shù)pthread_self,這個函數(shù)使POSIX線程庫中的一個函數(shù),通過這個函數(shù)可以獲得線程的ID,可是我們打印出來這個ID會發(fā)現(xiàn)這個ID是一個很大的數(shù)字。沒有得到我們想象的一個數(shù)字,其實這個ID是POSIX線程庫提供的一個數(shù)字,而linux內(nèi)核中也為這個線程提供了一個ID,這個ID可以通過gettid獲得,gettid是linux內(nèi)核提供的一個系統(tǒng)調(diào)用,Glibc沒有封裝函數(shù),只能通過系統(tǒng)調(diào)用實現(xiàn)。
POSIX:
#include <pthread>
pthread_t pthread_self(void);
linux系統(tǒng)調(diào)用:
#include <sys/types.h>
#include <sys/syscall.h>
pid_t gettid(void)
{
return syscall(SYS_gettid);
}
下面我們通過一個例程看下這兩個函數(shù)的區(qū)別。
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
pid_t gettid(void)
{
return syscall(SYS_gettid);
}
void *thread_a(void *arg)
{
printf("thread a entern");
pthread_t posix_tid;
pid_t linux_tid;
pid_t pid;
posix_tid = pthread_self();
linux_tid = gettid();
pid = getpid();
printf("pid = %x, posix_tid = %x, linux_tid = %xn", pid, posix_tid, linux_tid);
}
void *thread_b(void *arg)
{
printf("thread b entern");
pthread_t posix_tid;
pid_t linux_tid;
pid_t pid;
posix_tid = pthread_self();
linux_tid = gettid();
pid = getpid();
printf("pid = %x, posix_tid = %x, linux_tid = %xn", pid, posix_tid, linux_tid);
}
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 closen");
return 0;
}
程序運行結(jié)果:
thread a enter
pid = 3b89, posix_tid = b7fd4b90, linux_tid = 3b8a
thread b enter
pid = 3b89, posix_tid = b75d3b90, linux_tid = 3b8b
the main close
通過這個函數(shù)我們可以發(fā)現(xiàn)posix提供的這個ID不是很有規(guī)律,而linux內(nèi)核為線程提供的ID是經(jīng)跟在主進程進程號的數(shù)字,如上面程序中主進程ID為3b89而兩個線程的ID分別為3b8a,3b8b。
“本文由華清遠見http://www.embedu.org/index.htm提供”
來源:華清遠見0次