您的位置:首页 > 编程语言

线程编程基本知识介绍

2008-10-29 14:21 405 查看
线程编程基本知识介绍

线程编程基本知识介绍,常用线程函数列举
同进程相比,线程的应用比较复杂,如线程信号量的控制,线程属性的处理,同时还应注意哪些系统函数对线程来说是安全的,象exit()这样的系统调用在线程函数处理中是不能存在的。这篇文章只介绍线程编程最基本的部分及编程框架,更详细的部分请参考POSIX编程指南。
AIX支持以下的线程标准:
1) Single UNIX Specification, Version2
这一标准,包含POSIX线程标准,如IEEE 1003.1c标准,从AIX 5.1开始,完全支持Single
UNIX Specification, Version2 标准
2) Open Group UNIX98 Specification
同UNIX95相比,UNIX98有很大的扩展,基本覆盖POSIX线程定义部分

与线程相关的函数可以分类归结为以下几个部分:
1)线程管理函数,包括线程的创建,退出,函数为
pthread_create,pthread_join,,pthread_exit,,pthread_self, pthread_cancel,
pthread_setcancelstate, pthread_setcanceltype, pthread_testcancel,
pthread_cleanup_push, pthread_cleanup_pop
2) 线程同步处理函数
pthread_mutex_lock, pthread_mutex_unlock, pthread_mutex_destory
pthread_cond_init,pthread_cond_wait,pthread_cond_destory, pthread_attr_sedetachstate
pthread_attr_init, pthread_attr_desotry等
3) 线程数据处理函数
pthread_key_create, pthread_getspecific, pthread_setspecific

4) 线程调度函数
pthread_attr_setschedpolicy,pthread_attr_getschedpolicy, pthread_attr_setschedparam,
pthread_attr_getschedparam
以上只是介绍一些常用到线程函数,还有许多函数,不再一一列举,对于函数的参数、使用,请参考系统的函数手册。以下给出一个最简单的编程例子,同时说明在函数编程中正确退出线程的重要性,尤其是在应用程序需常见大量线程的情况下。
在AIX系统中编译连接POSIX的线程函数,使用编译器cc_r, xlc_r, 或xlC_r, 可根据应用编程语言的不同选择相匹配的编译器。
程序norexit.c线程正确退出并释放资源,原码如下:
#include <stdio.h>
#include <pthread.h>

void *thfunc(void *arg) {

-printf("In thread, thread id : %d/n", pthread_self());
-system(arg);

-pthread_exit(NULL);

}

int main(int argc, char **argv) {
-pthread_t thid;
-char commandstr[120];
-int rc,i=1;

-/* compose the command string */
-sprintf(commandstr, "ps aux | grep %s | grep -v grep", argv[0]);

-printf("Before create thread, the resource in this proccess: /n");
-system(commandstr);

-for (i=0; i<5; i++) {
--printf("/nSequence number : %d/n", i);
--rc = pthread_create(&thid, NULL, thfunc, (void *)commandstr);

--sleep(1);

--if(rc=pthread_join(thid,NULL)) {
---perror("wait thread exit error/n");
---printf("thread id: %d, errno: %d/n", thid, rc);
--}
--else {
---printf("thread ID : %d is exited/n", thid);
--} /* end if else */
} /* end for */

sleep(5);
printf("Exit the main flow/n");
system(commandstr);

return 0;

}
编译连接此程序:
xlc_r –o norexit norexit.c
运行norexit程序输出内容如下:
# ./norexit
Before create thread, the resource in this proccess:
root 19730 0.0 0.0 1108 1120 pts/0 A 02:51:33 0:00 ./norexit

Sequence number : 0
In thread, thread id : 258
root 19730 0.0 0.0 1160 1164 pts/0 A 02:51:33 0:00 ./norexit
thread ID : 258 is exited

Sequence number : 1
In thread, thread id : 259
root 19730 0.0 0.0 1180 1184 pts/0 A 02:51:33 0:00 ./norexit
thread ID : 259 is exited

Sequence number : 2
In thread, thread id : 260
root 19730 0.0 0.0 1180 1184 pts/0 A 02:51:33 0:00 ./norexit
thread ID : 260 is exited

Sequence number : 3
In thread, thread id : 261
root 19730 0.0 0.0 1180 1184 pts/0 A 02:51:33 0:00 ./norexit
thread ID : 261 is exited

Sequence number : 4
In thread, thread id : 262
root 19730 0.0 0.0 1180 1184 pts/0 A 02:51:33 0:00 ./norexit
thread ID : 262 is exited
Exit the main flow
root 19730 0.0 0.0 1180 1184 pts/0 A 02:51:33 0:00 ./norexit

从上面的输出结果,可以看出,norexit对内存的使用基本时稳定的,下面给出的例子是
线程未退出的情况下内存使用情况,程序名为unnorexit.c
#include <stdio.h>
#include <pthread.h>

void *thfunc(void *arg) {
-printf("In thread, thread id : %d/n", pthread_self());
-system(arg);
}

int main(int argc, char **argv) {
-pthread_t thid;
-char commandstr[120];
-int rc,i=1;

-/* compose the command string */
-sprintf(commandstr, "ps aux | grep %s | grep -v grep", argv[0]);

-printf("Before create thread, the resource in this proccess: /n");
-system(commandstr);

-for (i=0; i<5; i++) {
--printf("/nSequence number : %d/n", i);
--rc = pthread_create(&thid, NULL, thfunc, (void *)commandstr);

--sleep(1);

-} /* end for */

-sleep(5);
-printf("Exit the main flow/n");
-system(commandstr);

-return 0;

}
编译连接unnorexit.c程序
xlc_r –o unnorexit unnorexit.c
运行程序unnorexit,输出结果如下:
# ./unnorexit
Before create thread, the resource in this proccess:
root 18818 0.0 0.0 1112 1120 pts/0 A 02:56:54 0:00 ./unnorexit

Sequence number : 0
In thread, thread id : 258
root 18818 0.0 0.0 1164 1164 pts/0 A 02:56:54 0:00 ./unnorexit

Sequence number : 1
In thread, thread id : 515
root 18818 0.0 0.0 1188 1184 pts/0 A 02:56:54 0:00 ./unnorexit

Sequence number : 2
In thread, thread id : 772
root 18818 0.0 0.0 1204 1196 pts/0 A 02:56:54 0:00 ./unnorexit
Sequence number : 3
In thread, thread id : 1029
root 18818 0.0 0.0 1220 1208 pts/0 A 02:56:54 0:00 ./unnorexit

Sequence number : 4
In thread, thread id : 1286
root 18818 0.0 0.0 1236 1220 pts/0 A 02:56:54 0:00 ./unnorexit
Exit the main flow
root 18818 0.0 0.0 1236 1220 pts/0 A 02:56:54 0:00 ./unnorexit
从上面的输出结果,可以看到,随着线程的创建,内存使用逐渐增多。

总结
以上部分,简单介绍了POSIX的基本知识,即便程架构,详细的信息请参考AIX系统
编程手册及POSIX编程手册。

参考资料:
1) AIX 5L Porting Guide
2) Understanding C and C++ Application Development on AIX




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: