linux多线程的总结(pthread用法)
来源:互联网 发布:海迅开料软件 编辑:程序博客网 时间:2024/06/06 20:08
#include
int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,
void *(*start_rtn)(void),void *restrict arg);
Returns: 0 if OK, error number on failure
第一个参数为指向线程标识符的指针。
第二个参数用来设置线程属性。
第三个参数是线程运行函数的起始地址。
第四个参数是运行函数的参数。
当创建线程成功时,函数返回0,若不为0则说明创建线程失败,常见的错误返回代码为EAGAIN和EINVAL。前者表示系统限制创建新的线程,例如线程数目过多了;后者表示第二个参数代表的线程属性值非法.
pthread_create的用法:由于pthread库不是Linux系统默认的库,所以在使用pthread_create创建线程时,需要在编译中请加-lpthread参数,eg:gcc -o test -lpthrea test.c
例1:
#include "pthread.h"
#include "stdio.h"
void* thread_test(void* ptr)
{ while(1)
printf("i am pthread\n");
}
int main()
{
pthread_t pid;
pthread_create(&pid, NULL, test_thread, NULL);
while(1)
printf("i am main pthread\n");
return 0;
}
例2:
#include
#include
pthread_t id;
int ret;
void thread_1()
{
while(1)
{printf(“I am thread\n”);
sleep(1);
}
}
main()
{ret = pthread_create(&id,NULL,(void*)thread_1,NULL);
if(ret != 0)
printf("Create pthread error!\n");
while(1)
{
printf(“I am main thread\n”);
sleep(2);
}
}
例3:
#include
#include
#include
#include
void *thread_function(void *arg);
char message[] = "Hello World";
int main()
{
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
if (res != 0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result); //pthread_join 阻塞执行的线程直到某线程结束
if (res != 0)
{
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined, it returned %s\n", (char *)thread_result);
printf("Message is now %s\n", message);
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
printf("thread_function is running. Argument was %s\n", (char *)arg);
sleep(3);
strcpy(message, "Bye!");
pthread_exit("Thank you for the CPU time");
}
[root@plinux tmp]# cc -D_REENTRANT -I/usr/include/nptl thread2.c -o thread2 -L/usr/lib/nptl -lpthread
[root@plinux tmp]# ./thread2
thread_function is running. Argument was Hello World
Waiting for thread to finish...
Thread joined, it returned Thank you for the CPU time
Message is now Bye!
pthread_join()
void pthread_exit(void *retval)
int pthread_join(pthread_t pid, void **thread_return)
pthread_join()的调用者将挂起并等待th线程终止,retval是调用pthread_exit()的线程(线程ID为pid)的返回值,如果thread_return不为NULL,则*thread_return=retval。
需要注意的是一个线程仅允许唯一的另一个线程使用 pthread_join()等待本线程的终止,并且被等待的线程应该处于可join状态,即非DETACHED状态。
int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,
void *(*start_rtn)(void),void *restrict arg);
Returns: 0 if OK, error number on failure
第一个参数为指向线程标识符的指针。
第二个参数用来设置线程属性。
第三个参数是线程运行函数的起始地址。
第四个参数是运行函数的参数。
当创建线程成功时,函数返回0,若不为0则说明创建线程失败,常见的错误返回代码为EAGAIN和EINVAL。前者表示系统限制创建新的线程,例如线程数目过多了;后者表示第二个参数代表的线程属性值非法.
pthread_create的用法:由于pthread库不是Linux系统默认的库,所以在使用pthread_create创建线程时,需要在编译中请加-lpthread参数,eg:gcc -o test -lpthrea test.c
例1:
#include "pthread.h"
#include "stdio.h"
void* thread_test(void* ptr)
{ while(1)
printf("i am pthread\n");
}
int main()
{
pthread_t pid;
pthread_create(&pid, NULL, test_thread, NULL);
while(1)
printf("i am main pthread\n");
return 0;
}
例2:
#include
#include
pthread_t id;
int ret;
void thread_1()
{
while(1)
{printf(“I am thread\n”);
sleep(1);
}
}
main()
{ret = pthread_create(&id,NULL,(void*)thread_1,NULL);
if(ret != 0)
printf("Create pthread error!\n");
while(1)
{
printf(“I am main thread\n”);
sleep(2);
}
}
例3:
#include
#include
#include
#include
void *thread_function(void *arg);
char message[] = "Hello World";
int main()
{
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
if (res != 0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result); //pthread_join 阻塞执行的线程直到某线程结束
if (res != 0)
{
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined, it returned %s\n", (char *)thread_result);
printf("Message is now %s\n", message);
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
printf("thread_function is running. Argument was %s\n", (char *)arg);
sleep(3);
strcpy(message, "Bye!");
pthread_exit("Thank you for the CPU time");
}
[root@plinux tmp]# cc -D_REENTRANT -I/usr/include/nptl thread2.c -o thread2 -L/usr/lib/nptl -lpthread
[root@plinux tmp]# ./thread2
thread_function is running. Argument was Hello World
Waiting for thread to finish...
Thread joined, it returned Thank you for the CPU time
Message is now Bye!
pthread_join()
void pthread_exit(void *retval)
int pthread_join(pthread_t pid, void **thread_return)
pthread_join()的调用者将挂起并等待th线程终止,retval是调用pthread_exit()的线程(线程ID为pid)的返回值,如果thread_return不为NULL,则*thread_return=retval。
需要注意的是一个线程仅允许唯一的另一个线程使用 pthread_join()等待本线程的终止,并且被等待的线程应该处于可join状态,即非DETACHED状态。
- linux多线程的总结(pthread用法)
- linux多线程的总结(pthread用法)
- linux多线程的总结(pthread用法)
- linux多线程的总结(pthread用法)
- linux多线程的总结(pthread用法)
- linux多线程 pthread用法
- Linux多线程pthread知识总结
- Linux下的多线程编程(pthread)
- Linux下的多线程编程(pthread)
- Linux下的多线程编程(pthread)
- Linux的多线程编程-pthread
- Linux下的多线程编程(linux pthread)
- pthread 多线程用法
- Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解
- Pthread 多线程总结
- pthread/Linux多线程编程
- linux多线程pthread
- Linux 多线程 pthread
- HDU 4661 Message Passing 【Tree】
- MFC Windows 程序设计 第四章 菜单
- Myeclipse项目的调试与发布
- 进程调度
- MFC Windows 程序设计 第五 MFC集合类
- linux多线程的总结(pthread用法)
- MFC Windows 程序设计 第六章 文件I/O与串行化
- MFC Windows 程序设计 第七章 控件
- Effective C++读书笔记(四) 设计与声明
- 【parameter】oracle的隐含参数的查看
- Android系统移植与调试之------->如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
- 链表的创建与插入练习
- JAVA-设计模式之原型设计
- affine modle