C指针原理(47)-C应用技巧(2)
委托模型,即有一個(gè)BOSS線程,就是主線程,產(chǎn)生woker線程,boss線程和worker線程并發(fā)執(zhí)行。
BOSS線程的主要任務(wù)是創(chuàng)建worker線程,將工作線程放入隊(duì)列中,當(dāng)有工作可處理時(shí),喚醒 工作線程。
/ Create a new thread, starting with execution of START-ROUTINE getting passed ARG. Creation attributed come from ATTR. The new handle is stored in NEWTHREAD. /
extern int pthread_create (pthread_t restrict newthread,
const pthread_attr_t *restrict attr,
void (start_routine) (void ),
void restrict arg) THROW nonnull ((1, 3));
/ Obtain the identifier of the current thread. /
extern pthread_t pthread_self (void) THROW attribute ((const));
//返回調(diào)用該函數(shù)的當(dāng)前線程的pthread_t結(jié)構(gòu)指針
/ Make calling thread wait for termination of the thread TH. The
exit status of the thread is stored in THREAD_RETURN, if THREAD_RETURN
is not NULL.
This function is a cancellation point and therefore not marked with
THROW. */
extern int pthread_join (pthread_t th, void **__thread_return);//thread_return退出狀態(tài)
//pthread_join導(dǎo)致調(diào)用線程掛起它的執(zhí)行,直到目標(biāo)線程的結(jié)束。
main.c
#include <pthread.h>
#include <stdio.h>
//2個(gè)工作線程,分別是累加和累乘
void *mycompadd(void *xx){//參數(shù)必須為void *,然后進(jìn)行強(qiáng)制類型轉(zhuǎn)換
int sum=0;
int *x=(int *)(xx);
for (int i=0;i<*x;i++){
sum+=i;
}
printf(“add%d\n”,sum);
}
void *mycompchen(void *xx){//參數(shù)必須為void *,然后進(jìn)行強(qiáng)制類型轉(zhuǎn)換
int sum=1;
int *x=(int *)(xx);
for (int i=1;i<=x;i++){
sum=i;
}
printf(“chen%d\n”,sum);
}
int main(){
//main為boss線程,
pthread_t threada,threadb;
//創(chuàng)建worker線程,并執(zhí)行線程
int n=3;
pthread_create(&threada,NULL,mycompadd,&n);//線程,線程屬性,函數(shù),參數(shù)。如果有多個(gè)參數(shù),必須傳結(jié)構(gòu)指針
pthread_create(&threadb,NULL,mycompchen,&n);//線程,線程屬性,函數(shù),參數(shù)
//wait worker線程,并合并到BOSS線程來(lái)
pthread_join(threada,NULL);
pthread_join(threadb,NULL);
return(0);
}
執(zhí)行效果:
deepfuture@deepfuture-laptop:~/mytest$ gcc -lpthread -std=c99 -o main main.c
deepfuture@deepfuture-laptop:~/mytest$ ./main
add3
chen6
deepfuture@deepfuture-laptop:~/mytest$
C-多線程-取消及取消點(diǎn)
線程取消
編譯:
gcc -std=c99 -lpthread -o main main.c
deepfuture@deepfuture-laptop:~/mytest$ ./main
10000print:250
10000print:500
10000print:750
1add1
1chen1
thread0 已經(jīng)取消!
thread1 已經(jīng)取消!
2chen2
3chen6
4chen24
5chen120
6chen720
7chen5040
8chen40320
9chen362880
10chen3628800
11chen39916800
12chen479001600
13chen1932053504
14chen1278945280
15chen2004310016
16chen2004189184
17chen-288522240
18chen-898433024
19chen109641728
20chen-2102132736
21chen-1195114496
22chen-522715136
23chen862453760
24chen-775946240
25chen2076180480
thread2 不能被取消!br/>deepfuture@deepfuture-laptop:~/mytest$
C代碼
#include <pthread.h>
#include <stdio.h>
#define MAXTHREADS 3
void *mycompprint(void *xx){//參數(shù)必須為void *,然后進(jìn)行強(qiáng)制類型轉(zhuǎn)換
int oldstate,oldtype;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldstate);//設(shè)置線程是可以中止的。
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,&oldtype);//設(shè)置線程推遲中止,PTHREAD_CANCEL_DEFERRED為默認(rèn)值。
int *x=(int *)(xx);
for (int i=1;i<*x;i++){
if ((i%250)==0) {//如果i為250的倍數(shù)則取消
printf("%dprint:%d\n",*x,i);
pthread_testcancel();//pthread_testcancel()檢測(cè)是否需要取消,設(shè)置取消點(diǎn),如果有掛起的取消請(qǐng)求,則在此處中止本線程
}
}
}
void *mycompadd(void *xx){//參數(shù)必須為void *,然后進(jìn)行強(qiáng)制類型轉(zhuǎn)換
int oldstate,oldtype;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldstate);//設(shè)置線程是可以中止的。
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&oldtype);//設(shè)置線程線程立即中止,PTHREAD_CANCEL_ASYNCHRONOUS表示線程立即終止。
int sum=0;
int *x=(int *)(xx);
int y;
for (int i=1;i<=*x;i++){
sum+=i;
printf("%dadd%d\n",i,sum);
}
}
void *mycompchen(void *xx){//參數(shù)必須為void *,然后進(jìn)行強(qiáng)制類型轉(zhuǎn)換
int oldstate,oldtype;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&oldstate);//設(shè)置線程不能中止的。
int sum=1;
int *x=(int *)(xx);
for (int i=1;i<=x;i++){
sum=i;
printf("%dchen%d\n",i,sum);
}
}
int main(){
//線程分離后,不能再合并
//main為boss線程,
pthread_t threads[MAXTHREADS];//創(chuàng)建線程池
void *status;
//創(chuàng)建worker線程,并執(zhí)行線程
int n1=25;
int n2=10000;
pthread_create(&(threads[0]),NULL,mycompprint,&n2);
pthread_create(&(threads[1]),NULL,mycompadd,&n1);
pthread_create(&(threads[2]),NULL,mycompchen,&n1);
for (int i=0;i<MAXTHREADS;i++){
pthread_cancel(threads[i]);
}
for (int i=0;i<MAXTHREADS;i++){
pthread_join(threads[i],&status); //wait worker線程,并合并到BOSS線程來(lái)
if (status==PTHREAD_CANCELED){
printf(“thread%d 已經(jīng)取消!\n”,i);
}
else{
printf(“thread%d 不能被取消!\n”,i);
}
}
return(0);
}
c-多線程-中止前清理
gcc -lpthread -std=c99 -o main main.c
deepfuture@deepfuture-laptop:~/mytest$ ./main
1chen1
2chen2
3chen6
4chen24
5chen120
6chen720
7chen5040
8chen40320
9chen362880
10chen3628800
11chen39916800
12chen479001600
13chen1932053504
14chen1278945280
15chen2004310016
16chen2004189184
17chen-288522240
18chen-898433024
19chen109641728
20chen-2102132736
21chen-1195114496
22chen-522715136
23chen862453760
24chen-775946240
25chen2076180480
1add1
10000print:250
clear:10000
thread0 已經(jīng)取消!
thread1 已經(jīng)取消!
thread2 不能被取消!
C代碼
#include <pthread.h>
#include <stdio.h>
#define MAXTHREADS 3
void myclear(void x){
printf(“clear:%d\n”,((int)x));
}
void *mycompprint(void *xx){//參數(shù)必須為void *,然后進(jìn)行強(qiáng)制類型轉(zhuǎn)換
int oldstate,oldtype;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldstate);//設(shè)置線程是可以中止的。
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,&oldtype);//設(shè)置線程推遲中止,PTHREAD_CANCEL_DEFERRED為默認(rèn)值。
int *x=(int *)(xx);
void *xxx=(void *)x;
pthread_cleanup_push(myclear,xxx);//壓入線程清理堆棧,堆棧包含指向取消過程中執(zhí)行例程的指針,即中止前執(zhí)行一個(gè)清理。myclear為例程名,x為傳給例程的參數(shù)
for (int i=1;i<*x;i++){
if ((i%250)==0) {//如果i為250的倍數(shù)則取消
printf("%dprint:%d\n",*x,i);
pthread_testcancel();//pthread_testcancel()檢測(cè)是否需要取消,設(shè)置取消點(diǎn),如果有掛起的取消請(qǐng)求,則在此處中止本線程
}
}
pthread_cleanup_pop(0); //從調(diào)用線程清理堆棧的頂部移走清理函數(shù)指針,但并不執(zhí)行它,pthread_testcancel()檢測(cè)不到取消請(qǐng)求,表示目前不需要取消,所以移走它。pthread_cleanup_pop(1)移走并執(zhí)行它,即使并沒有中止線程;
}
void *mycompadd(void *xx){//參數(shù)必須為void *,然后進(jìn)行強(qiáng)制類型轉(zhuǎn)換
int oldstate,oldtype;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldstate);//設(shè)置線程是可以中止的。
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&oldtype);//設(shè)置線程線程立即中止,PTHREAD_CANCEL_ASYNCHRONOUS表示線程立即終止。
int sum=0;
int *x=(int *)(xx);
int y;
for (int i=1;i<=*x;i++){
sum+=i;
printf("%dadd%d\n",i,sum);
}
}
void *mycompchen(void *xx){//參數(shù)必須為void *,然后進(jìn)行強(qiáng)制類型轉(zhuǎn)換
int oldstate,oldtype;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&oldstate);//設(shè)置線程不能中止的。
int sum=1;
int *x=(int *)(xx);
for (int i=1;i<=x;i++){
sum=i;
printf("%dchen%d\n",i,sum);
}
}
int main(){
//線程分離后,不能再合并
//main為boss線程,
pthread_t threads[MAXTHREADS];//創(chuàng)建線程池
void *status;
//創(chuàng)建worker線程,并執(zhí)行線程
int n1=25;
int n2=10000;
pthread_create(&(threads[0]),NULL,mycompprint,&n2);
pthread_create(&(threads[1]),NULL,mycompadd,&n1);
pthread_create(&(threads[2]),NULL,mycompchen,&n1);
for (int i=0;i<MAXTHREADS;i++){
pthread_cancel(threads[i]);
}
for (int i=0;i<MAXTHREADS;i++){
pthread_join(threads[i],&status); //wait worker線程,并合并到BOSS線程來(lái)
if (status==PTHREAD_CANCELED){
printf(“thread%d 已經(jīng)取消!\n”,i);
}
else{
printf(“thread%d 不能被取消!\n”,i);
}
}
return(0);
}
linux-線程優(yōu)先級(jí)
C代碼
#include <pthread.h>
#include <stdio.h>
#define MAXTHREADS 3
void myclear(void x){
printf(“clear:%d\n”,((int)x));
}
void *mycompprint(void *xx){//參數(shù)必須為void *,然后進(jìn)行強(qiáng)制類型轉(zhuǎn)換
int oldstate,oldtype;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldstate);//設(shè)置線程是可以中止的。
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,&oldtype);//設(shè)置線程推遲中止,PTHREAD_CANCEL_DEFERRED為默認(rèn)值。
int *x=(int *)(xx);
void *xxx=(void *)x;
pthread_cleanup_push(myclear,xxx);//壓入線程清理堆棧,堆棧包含指向取消過程中執(zhí)行例程的指針,即中止前執(zhí)行一個(gè)清理。myclear為例程名,x為傳給例程的參數(shù)
for (int i=1;i<*x;i++){
if ((i%60)==0) {//如果i為250的倍數(shù)則取消
printf("%dprint:%d\n",*x,i);
pthread_testcancel();//pthread_testcancel()檢測(cè)是否需要取消,設(shè)置取消點(diǎn),如果有掛起的取消請(qǐng)求,則在此處中止本線程
}
}
pthread_cleanup_pop(0); //從調(diào)用線程清理堆棧的頂部移走清理函數(shù)指針,但并不執(zhí)行它,pthread_testcancel()檢測(cè)不到取消請(qǐng)求,表示目前不需要取消,所以移走它。pthread_cleanup_pop(1)移走并執(zhí)行它,即使并沒有中止線程;
}
void *mycompadd(void *xx){//參數(shù)必須為void *,然后進(jìn)行強(qiáng)制類型轉(zhuǎn)換
int oldstate,oldtype;
int sum=0;
int *x=(int *)(xx);
int y;
pthread_attr_t attr1;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&oldstate);//設(shè)置線程不能中止的。
for (int i=1;i<=*x;i++){
sum+=i;
printf("%dadd%d\n",i,sum);
}
}
void *mycompchen(void *xx){//參數(shù)必須為void *,然后進(jìn)行強(qiáng)制類型轉(zhuǎn)換
int oldstate,oldtype;
size_t size;
void *addr;
int priority;
pthread_attr_t attr1;
struct sched_param param;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&oldstate);//設(shè)置線程不能中止的。
pthread_getattr_np(pthread_self(),&attr1);//獲取線程屬性。
pthread_attr_getstack(&attr1,&addr,&size);//線程屬性,地址,大小
param.sched_priority=sched_get_priority_min(SCHED_RR);//SCHED_RR策略的sched_get_priority_min最小優(yōu)先值
pthread_setschedparam(pthread_self(),SCHED_RR,¶m);//動(dòng)態(tài)設(shè)置調(diào)度策略
//pthread_setschedprio(pthread_self(),sched_get_priority_min(SCHED_RR)); //另一種動(dòng)態(tài)設(shè)置調(diào)度優(yōu)先級(jí)的方法
printf(“size:%d\n”,size); //輸出線程堆棧大小
int sum=1;
int *x=(int *)(xx);
for (int i=1;i<=x;i++){
sum=i;
printf("%dchen%d\n",i,sum);
}
}
int main(){
//線程分離后,不能再合并
//main為boss線程,
pthread_t threads[MAXTHREADS];//創(chuàng)建線程池
void *status;
pthread_attr_t attr;
//創(chuàng)建worker線程,并執(zhí)行線程
int n1=25;
int n2=10000;
int priority;
struct sched_param param;
//靜態(tài)設(shè)置線程threads[1]調(diào)度及相關(guān)屬性,優(yōu)先值越小,優(yōu)先級(jí)越高
pthread_attr_init(&attr);
priority=sched_get_priority_max(SCHED_RR);//SCHED_RR策略的sched_get_priority_max最大優(yōu)先值
//SCHED_RR輪詢調(diào)度,SCHED_FIFO先進(jìn)先出,執(zhí)行線程直到完成,SCHED_OTHER其他調(diào)度。sched_get_prority_max、sched_get_prority_min取得調(diào)度策略的最大優(yōu)先值、最小優(yōu)先值
param.sched_priority=priority;//設(shè)置param的優(yōu)先級(jí)成員
pthread_attr_setschedparam(&attr,¶m);//通過param設(shè)置優(yōu)先級(jí)
pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);//PTHREAD_EXPLICIT_SCHED設(shè)置調(diào)度屬性為屬性對(duì)象的調(diào)度屬性,THREAD_INHERIT_EXPLICIT_SCHED為繼承調(diào)度屬性
pthread_create(&(threads[0]),NULL,mycompprint,&n2);
pthread_create(&(threads[1]),&attr,mycompadd,&n1);
pthread_create(&(threads[2]),NULL,mycompchen,&n1); \
for (int i=0;i<MAXTHREADS;i++){
pthread_cancel(threads[i]);
}
sleep(1);
for (int i=0;i<MAXTHREADS;i++){
pthread_join(threads[i],&status); //wait worker線程,并合并到BOSS線程來(lái)
if (status==PTHREAD_CANCELED){
printf(“thread%d 已經(jīng)取消!\n”,i);
}
else{
printf(“thread%d 不能被取消!\n”,i);
}
}
return(0);
}
linux-C直接調(diào)用SO動(dòng)態(tài)庫(kù)和生成SO動(dòng)態(tài)庫(kù)的函數(shù)
C代碼
#include <stdio.h>
#include <dlfcn.h>
int main(void){
int (*myadd)(int a,int b);//fuction pointer
void *handle;
handle=dlopen("./libmyadd.so",RTLD_LAZY);//open lib file
myadd=dlsym(handle,“output”);//call dlsym function
int result=myadd(1,2);
dlclose(handle);
printf("%d\n",result);
}
以上為調(diào)用程序test8.c,以下為庫(kù)程序test7.c
C代碼
int output(int a,int b){
int x=a+b;
return x;
}
knoppix@Microknoppix:/mnt-system/deepfuture$ gcc -shared -o libmyadd.so test7.c
knoppix@Microknoppix:/mnt-system/deepfuture$ gcc -ldl -o test8 test8.c
knoppix@Microknoppix:/mnt-system/deepfuture$ ./test8
3
總結(jié)
以上是生活随笔為你收集整理的C指针原理(47)-C应用技巧(2)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android sdk64位资源,and
- 下一篇: android创建空文件,ADT 更新