`
- 浏览:
1084116 次
- 性别:
- 来自:
北京
-
#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状态。
http://www.linuxdiyf.com/viewarticle.php?id=92229
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
Linux多线程编程手册,介绍了Linux下pthread的使用方法
对于C++而言,当我们需要使用多线程时,可以使用boost::thread库或者自从C++ 11开始支持的std::thread,也可以使用操作系统相关的线程API,如在Linux上,可以使用pthread库。除此之外,还可以使用omp来使用多线程。...
常见的多线程API和用法 pthread_self函数 pthread_create函数 pthread_exit函数 pthread_join函数 pthread_detach函数 pthread_equal函数
所以,多线程环境中,应尽量少用,或者不使用exit函数,取而代之使用pthread_exit函数,将单个线程退出。任何线程里exit导致进程退出,其他线程未工作结束,主控线程退出时不能return或exit。 另注意,pthread_exit...
在Linux操作系统中,线程同步是多线程编程中的一个核心概念,它确保了多个线程在访问共享资源时的正确性与一致性,避免了诸如数据竞争和竞态条件等问题。为了实现这一目标,Linux提供了一系列强大的线程同步机制和...
所以,互斥锁实质上是操作系统提供的一把“建议锁”(又称“协同锁”),建议程序中有多线程访问共享资源的时候使用该机制。但,并没有强制限定。 因此,即使有了mutex,如果有线程不按规则来访问数据,依然会造成...
支持windows下使用的pthread库,设置包含目录和库目录即可,使用方法和linux下一样。。。
本文档介绍Linux下函数API的使用方法和检索方法,比如在进行多线程编程时,只知道函数名包含pthread关键字,但不记得或不知道具体的函数名,阅读本文档就可以找到相应的函数
在Windows上是使用系统win32 api创建和管理线程,Linux和Mac通常使用pthread,尽管Windows也可以使用第三方的pthread库,但这样库的依赖会比较多,项目的部署会麻烦些,最佳的方法应该还是写跨平台代码,通过宏区分...
4.4.1 多线程编程实例 127 4.4.2 Linux下线程创建函数pthread_create() 129 4.4.3 线程的结束函数pthread_join()和pthread_exit() 129 4.4.4 线程的属性 130 4.4.5 线程间的互斥 132 4.4.6 线程中使用信号...
4.4.1 多线程编程实例 127 4.4.2 Linux下线程创建函数pthread_create() 129 4.4.3 线程的结束函数pthread_join()和pthread_exit() 129 4.4.4 线程的属性 130 4.4.5 线程间的互斥 132 4.4.6 线程中使用信号...
这是什么该存储库包含一些基于 C 的程序,用于测试 Linux 环境中的多线程处理。 具体来说,我们使用多线程遍历整个搜索空间,以便从给定的散列摘要中恢复消息。 线程是通过以下方法创建的线程基于clone(2)的轻量级...
mtstates 从多个线程调用脚本语言的解释器状态。 这个包提供了一种从Lua内部创建新Lua状态的方法,以便在任意线程中使用它们。 该实现独立于基础线程库...对于示例用作低级多线程实现。 第一个示例:构造一个状态,
4.4.1 多线程编程实例 127 4.4.2 Linux下线程创建函数pthread_create() 129 4.4.3 线程的结束函数pthread_join()和pthread_exit() 129 4.4.4 线程的属性 130 4.4.5 线程间的互斥 132 4.4.6 线程中使用信号...
相信需要了解这方面的知识的小伙伴,已经基本对进程间通信和线程间通信有了一定了解。例如,进程间通信的机制之一:共享内存(在这里不做详解):多个进程可同时访问同一块内存。如果不对访问这块内存的临界区进行...
SDL2.2.0.1的移植,包含SDL的每个参数的意思,包含交叉编译器的安装,包含移植...--enable-pthread-sem 使用多线程信号 --enable-input-events 使用Linux-2.4统一输入接口 --enable-input-tslib 使用Touchscreen库输入
建议生产部署在linux下运行多进程模式,因为运行在多线程模式运行一段时间后报错,pthreads has detected that the core\lib\Pthread could not be started, the system lacks the necessary resources or the system...
改用 C++ 标准线程或 libunistd 的 POSIX pthread。 用法:在VC ++项目中设置包含路径,以包含目录libunistd / unistd(unistd.h文件所在的目录)。 在任何无法在 VC++ 中编译的 Linux 代码中,在头文件的顶部包含...
gthread:符合POSIX pthread的多线程和C ++包装器 gsem:符合POSIX标准的信号灯 gsocket:套接字API和C ++包装器 gtimer:高分辨率计时器 gmmap:内存映射文件 glist:简单的双向链表 gque:简单的循环队列 gmbox:...
第1 章Linux操作系统概述................... .......................................................................... 2 1.1 Linux发展历史........................................................ 2 ...