gogo专业大尺度亚洲高清人体,美女张开双腿让男生桶,亚洲av无码一区二区三区鸳鸯影院,久久久久国产精品人妻

當(dāng)前位置:首頁 > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > Linux下多線程機(jī)制

Linux下多線程機(jī)制 時(shí)間:2018-09-26      來源:未知

1 線程不能獨(dú)立運(yùn)行,要依附于進(jìn)程

2 如果創(chuàng)建一個(gè)子線程只需要重新分配?臻g

3 多個(gè)線程可以并行運(yùn)行

4 線程之間可以有共同的全局變量(全局區(qū),任何線程都可以訪問)

5 多線程效率高

如何創(chuàng)建子線程(在進(jìn)程中創(chuàng)建線程)

#include

int pthread_create(pthread_t *thread, pthread_arrt_t *attr, void *(*start_routine)(void *), void *arg);

功能:創(chuàng)建一個(gè)子線程

參數(shù):

thread [出參],當(dāng)程序執(zhí)行此函數(shù),此函數(shù)會(huì)傳出一個(gè)值,線程的id

attr [入?yún),通常為NULL, 線程的屬性,如果為NULL, 屬性默認(rèn)(線程優(yōu)先級(jí),線程堆棧大小....)

start_routine 函數(shù)指針,需要傳進(jìn)來一個(gè)函數(shù)名,然后會(huì)自動(dòng)執(zhí)行此函數(shù),

此函數(shù)就是線程需要執(zhí)行的程序

arg 此參數(shù)專門給第三個(gè)參數(shù)start_routine使用的,此參數(shù),作為start_routine函數(shù)的參數(shù)

int process(int (*p)(int, int), int a, int b)

{

p(a, b);

}

創(chuàng)建一個(gè)線程實(shí)例:

#include

#include

void *fun(void *p)

{

while(1)

{

printf("thread 1 running\n");

sleep(1);

}

}

int main()

{

pthread_t id;

pthread_create(&id, NULL, fun, NULL);

printf("%lu\n", id);

while(1) //目的:讓主進(jìn)程不結(jié)束

{

;

}

}

編譯時(shí): gcc -o hello hello.c -lpthread //多線程是一個(gè)第三庫(kù)函數(shù),所以要加-lpthread

多線程的好處:

要實(shí)現(xiàn) 1 接收鍵盤輸入 2 同時(shí)每隔一秒鐘打印一下家中的溫度

pthread_join(); ///函數(shù)功能:主進(jìn)程如果執(zhí)行到此函數(shù),將阻塞,等待子線程結(jié)束

#include

#include

void *fun(void *p)

{

while(1)

{

printf("thread 1 running\n");

sleep(1);

}

}

int main()

{

pthread_t id;

pthread_create(&id, NULL, fun, NULL);

printf("%lu\n", id);

pthread_join(id, NULL); //阻塞,等待子線程結(jié)束, 節(jié)省cpu資源

}

線程參數(shù)

//////////////實(shí)例:傳遞字符串給線程/////////////////////

#include

#include

void *fun(void *p)

{

//char *q = (char *)p;

while(1)

{

printf("%s\n", (char *)p);

sleep(1);

}

}

int main()

{

char s[] = "hello world";

pthread_t id;

pthread_create(&id, NULL, fun, s);

printf("%lu\n", id);

pthread_join(id, NULL); //阻塞,等待子線程結(jié)束, 節(jié)省cpu資源

}

//////////////實(shí)例:傳遞整形變量給線程/////////////////////

#include

#include

void *fun(void *p)

{

int *q = (int *)p;

while(1)

{

printf("%d\n", *q);

sleep(1);

}

}

int main()

{

int a = 100;

pthread_t id;

pthread_create(&id, NULL, fun, &a);

printf("%lu\n", id);

pthread_join(id, NULL); //阻塞,等待子線程結(jié)束, 節(jié)省cpu資源

}

void *p ///無類型指針,可以定義變量,但不可以使用(*p = 100, p++, p--)

///無類型指針只能賦值給另一個(gè)帶類型的指針變量

線程的同步與互斥

同步(按照預(yù)想的順序執(zhí)行)

M->Y->M->Y->M->Y

M->YYY->M->YYY......

互斥

你用,我不能用(如:網(wǎng)絡(luò)打印機(jī),A 打印時(shí), B不可以打印)

/////互斥例子

#include

#include

int a[10] = { 0 }; ///共享資源

int i;

void *fun1()

{

while(1)

{

int i;

for(i = 0; i < 10; i++)

{

a[i] = i;

}

sleep(2);

for(i = 0; i < 10; i++)

{

printf("a[%d] is %d\n", i, a[i]);

}

}

}

void *fun2()

{

while(1)

{

sleep(1);

for(i = 0; i < 10; i++)

{

a[i] = 1;

}

}

}

int main()

{

pthread_t id1, id2;

pthread_create(&id1, NULL, fun1, NULL);

pthread_create(&id2, NULL, fun2, NULL);

pthread_join(id1, NULL);

}

//////線程同步(mutex)

A 使用共享資源,加鎖,如果這時(shí)B也使用共享資源,

B也加鎖,但是鎖已經(jīng)被A占用,B只能等,一旦A解鎖,B運(yùn)行,加鎖,使用共享資源

//////互斥鎖,用來解決共享資源同步的問題,(互斥鎖初始化)

pthread_mutex_t 互斥鎖類型結(jié)構(gòu)體

1 創(chuàng)建互斥鎖(初始化互斥鎖)

pthread_mutex_t mutex;

int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr *mutexattr);

mutex [出參] 創(chuàng)建互斥鎖,會(huì)將新建的互斥鎖信息傳遞給mutex變量

mutexattr 互斥鎖屬性,默認(rèn)為NULL

例:

pthread_mutex_init(&mutex, NULL); //創(chuàng)建并初始化互斥鎖

2 加鎖

一旦某個(gè)線程使用共享資源,就加鎖

int pthread_mutex_lock(pthread_mutex_t *mutex); //如果加鎖不成功(某個(gè)線程已經(jīng)加鎖),阻塞

3 解鎖

int pthread_mutex_unlock(pthread_mutex_t *mutex); //如果某個(gè)線程正在等待加鎖,那么這線程進(jìn)入就緒態(tài)

#include

#include

int a[10] = { 0 }; ///共享資源

int i;

pthread_mutex_t mutex;

void *fun1()

{

while(1)

{

int i;

pthread_mutex_lock(&mutex);

for(i = 0; i < 10; i++)

{

a[i] = i;

}

sleep(2);

for(i = 0; i < 10; i++)

{

printf("a[%d] is %d\n", i, a[i]);

}

pthread_mutex_unlock(&mutex);

sleep(1);

}

}

void *fun2()

{

while(1)

{

sleep(1);

pthread_mutex_lock(&mutex);

for(i = 0; i < 10; i++)

{

a[i] = 1;

}

pthread_mutex_unlock(&mutex);

}

}

int main()

{

pthread_t id1, id2;

pthread_mutex_init(&mutex, NULL);

pthread_create(&id1, NULL, fun1, NULL);

pthread_create(&id2, NULL, fun2, NULL);

pthread_join(id1, NULL);

}

////////信號(hào)量 (semaphore 簡(jiǎn)寫 sem)

同樣可以解決共享資源互斥和同步的問題

信號(hào)量可以控制多個(gè)共享資源被訪問互斥的問題

#include

sem_t sem;

1 創(chuàng)建信號(hào)量(初始化信號(hào)量)

int sem_init(sem_t *sem, int pshared, int value);

sem [出參], 在創(chuàng)建信號(hào)量時(shí),傳出的信號(hào)量結(jié)構(gòu)體

pshared 通常寫0,代表此信號(hào)量在多線程之間使用

value 共享資源個(gè)數(shù)

sem_init(&sem, 0, 3);

sem_init(&sem, 0, 1);

sem_init(&sem, 0, 0);

2 請(qǐng)求信號(hào)量

sem_wait(&sem); //如果共享資源個(gè)數(shù)不為0, 請(qǐng)求成功,使用共享資源,然后共享資源個(gè)數(shù)-1

//當(dāng)共享資源個(gè)數(shù)為0時(shí),請(qǐng)求失敗,阻塞

3 釋放信號(hào)量

sem_post(&sem); //釋放信號(hào)量,如果有線程正在阻塞等待信號(hào)量,那么阻塞解除,

//如果沒有線程正在等待信號(hào)量,共享資源個(gè)數(shù)+1

四個(gè)線程共享信號(hào)量實(shí)例

sem_init(&sem, 0, 3); //表示共享資源個(gè)數(shù)有三個(gè)

線程A 調(diào)用sem_wait(&sem); //共享資源個(gè)數(shù)-1, 2

線程B 調(diào)用sem_wait(&sem); //共享資源個(gè)數(shù)-1, 1

線程C 調(diào)用sem_wait(&sem); //共享資源個(gè)數(shù)-1, 0

線程D 調(diào)用sem_wait(&sem); //共享資源個(gè)數(shù)已經(jīng)為0, 線程D阻塞

如果線程A 調(diào)用sem_post(&sem); //線程D 解除阻塞

線程屬性

線程可以設(shè)置堆棧大小,可以設(shè)置線程優(yōu)先級(jí)

默認(rèn)情況堆棧大小(有些系統(tǒng)1M, 有些2M, 有些4M, 有些8M)

如果定義一個(gè)局部變量占用空間特別大,要改堆棧大小

測(cè)試線程堆棧大小

#include

#include

void *fun()

{

int a[1000000]; //約等于 4M 1M 1024k 1k 1024B

while(1)

{

printf("thread 1\n");

sleep(1);

}

}

int main()

{

pthread_t id1;

pthread_create(&id1, NULL, fun, NULL);

pthread_join(id1, NULL);

}

pthread_create(pthread_t *thread, pthread_attr_t *attr, void*(*start_routine)(void *), void *arg);

第二個(gè)參數(shù) pthread_attr_t *attr; 線程屬性,默認(rèn)NULL

1 獲得線程默認(rèn)屬性,賦值給attr

pthread_attr_t attr;

pthread_attr_init(&attr); // 功能:獲取線程的默認(rèn)屬性,會(huì)更改attr的值,給一個(gè)默認(rèn)值

2 設(shè)置線程的堆棧大小屬性

pthread_attr_setstacksize(&attr, 14000000); ///功能:設(shè)置堆棧大小屬性

#include

#include

void *fun()

{

int a[1000000]; //約等于 4M 1M 1024k 1k 1024B

while(1)

{

printf("thread 1\n");

sleep(1);

}

}

int main()

{

pthread_t id1;

pthread_attr_t attr;

pthread_attr_init(&attr); // 功能:獲取線程的默認(rèn)屬性,會(huì)更改attr的值,給一個(gè)默認(rèn)值

pthread_attr_setstacksize(&attr, 14000000); ///功能:設(shè)置堆棧大小屬性

pthread_create(&id1, &attr, fun, NULL);

pthread_join(id1, NULL);

}

上一篇:Exynos 4412 看門狗定時(shí)器中斷

下一篇:Android異步加載AsyncTask的使用

熱點(diǎn)文章推薦
華清學(xué)員就業(yè)榜單
高薪學(xué)員經(jīng)驗(yàn)分享
熱點(diǎn)新聞推薦
前臺(tái)專線:010-82525158 企業(yè)培訓(xùn)洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠(yuǎn)見科技集團(tuán)有限公司 版權(quán)所有 ,京ICP備16055225號(hào)-5,京公海網(wǎng)安備11010802025203號(hào)

回到頂部