typedef unsigned long int pthread_t;
//come from /usr/include/bits/pthreadtypes.h

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);创建新的线程

pthread_t pthread_self(void);获取本线程的线程ID

int pthread_equal(pthread_t t1, pthread_t t2);判断两个线程ID是否指向同一线程

int pthread_once(pthread_once_t *once_control, void (*init_routine) (void));用来保证init_routine线程函数在进程中只执行一次。

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h> int* thread_func(void* arg)
{
pthread_t new_thid;
new_thid = pthread_self();//打印线程自己的线程ID
printf("the new thread, ID is %lu\n", new_thid); return NULL;
} int main()
{
pthread_t thid; printf("main thread, ID is %lu\n", pthread_self());//打印主线程自己的线程ID if (pthread_create(&thid, NULL, (void*)thread_func, NULL) != )
{
printf("create thread failed\n");
exit();
} sleep(); return ;
}

某些情况下,函数执行次数要被限制为1次,这种情况下,要使用pthread_once,代码示例:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h> pthread_once_t once = PTHREAD_ONCE_INIT; void run(void)
{
printf("function run is running in thread:%lu\n", pthread_self());
} int* thread1(void* arg)
{
pthread_t new_thid;
new_thid = pthread_self();
printf("current thread ID is %lu\n", new_thid);
pthread_once(&once, run);
printf("thread1 end\n");
return NULL;
} int* thread2(void* arg)
{
pthread_t new_thid;
new_thid = pthread_self();
printf("current thread ID is %lu\n", new_thid);
pthread_once(&once, run);
printf("thread2 end\n");
return NULL;
} int main()
{
pthread_t thid1, thid2; printf("main thread, ID is %lu\n", pthread_self()); pthread_create(&thid1, NULL, (void*)thread1, NULL);
pthread_create(&thid2, NULL, (void*)thread2, NULL); sleep();
printf("main thread exit\n"); return ;
}

运行结果:

main thread, ID is 3076200128
current thread ID is 3067804480
function run is running in thread:3067804480
thread2 end
current thread ID is 3076197184
thread1 end
main thread exit

虽然在thread1 跟thread2中都调用了run函数,但是run函数只执行了一次。

线程的创建,pthread_create,pthread_self,pthread_once的更多相关文章

  1. 线程的创建pthread_create.c

    #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <errno.h&g ...

  2. 关于linux的一点好奇心(五):进程线程的创建

    一直以来,进程和线程的区别,这种问题一般会被面试官拿来考考面试者,可见这事就不太简单.简单说一点差异是,进程拥有独立的内存资源信息,而线程则共享父进程的资源信息.也就是说线程不拥有内存资源,所以对系统 ...

  3. Linux多线程编程——线程的创建与退出

    POSIX线程标准:该标准定义了创建和操纵线程的一整套API.在类Unix操作系统(Unix.Linux.Mac OS X等)中,都使用Pthreads作为操作系统的线程.Windows操作系统也有其 ...

  4. 三十六、Linux 线程——线程基本概念及线程的创建和终止

    36.1 线程介绍 36.1.1 线程的基本概念 进程是资源管理的最小单位,线程是程序执行的最小单位 每个进程都有自己的数据段.代码段和堆栈段. 线程通常叫做轻型的进程,它包含独立的栈和 CPU 寄存 ...

  5. POSIX 线程的创建与退出

    前言 创建线程: pthread_create() 退出线程: pthread_exit()return pthread_cancel() 线程的创建 使用多线程,首先就需要创建一个新线程.那么线程是 ...

  6. Linux线程的创建

    一.线程与进程的区别 1.线程自己不拥有系统资源,只拥有一点儿在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所拥有的全部资源. 2.进程是资源分配的基本单位.所有与该进程有关的资源,都 ...

  7. 线程概念( 线程的特点,进程与线程的关系, 线程和python理论知识,线程的创建)

    参考博客: https://www.cnblogs.com/xiao987334176/p/9041318.html 线程概念的引入背景 进程 之前我们已经了解了操作系统中进程的概念,程序并不能单独运 ...

  8. python 全栈开发,Day41(线程概念,线程的特点,进程和线程的关系,线程和python 理论知识,线程的创建)

    昨日内容回顾 队列 队列 : 先进先出.数据进程安全 队列实现方式: 管道 + 锁 生产者消费者模型 : 解决数据供需不平衡 管道 双向通信 数据进程不安全 EOFError: 管道是由操作系统进行引 ...

  9. python全栈开发,Day41(线程概念,线程的特点,进程和线程的关系,线程和python理论知识,线程的创建)

    昨日内容回顾 队列 队列:先进先出.数据进程安全 队列实现方式:管道+锁 生产者消费者模型:解决数据供需不平衡 管道 双向通信,数据进程不安全 EOFError: 管道是由操作系统进行引用计数的 必须 ...

随机推荐

  1. 09: python基础补充

    1.1 闭包 1.闭包概念 1. 在一个外函数中定义了一个内函数,内函数里运用了外函数的临时变量,并且外函数的返回值是内函数的引用,这样就构成了一个闭包 2. 一般情况下,在我们认知当中,如果一个函数 ...

  2. 20145127《java程序设计》第四周学习总结

    教材学习内容总结 第六章 继承与多态 6.1 何为继承 0.面向对象中,子类继承父类,避免城府的行为定义.正确判断使用继承的时机,以及继承之后如何活用多态,才是学习继承时的重点. 1.继承:避免多个类 ...

  3. 基于Android应用《玩转英语》(总报告)

                                                                         基于Android应用<玩转英语>   摘  要 ...

  4. Java序列化流-ObjectOutputStream、ObjectInputStream

    Java对象流的基本概念: 实例代码: 实体类User: import java.io.Serializable; /** * @author zsh * @company wlgzs * @crea ...

  5. 不到 200 行代码,教你如何用 Keras 搭建生成对抗网络(GAN)【转】

    本文转载自:https://www.leiphone.com/news/201703/Y5vnDSV9uIJIQzQm.html 生成对抗网络(Generative Adversarial Netwo ...

  6. POJ 1260 Pearls (斜率DP)题解

    思路: 直接DP也能做,这里用斜率DP. dp[i] = min{ dp[j] + ( sum[i] - sum[j] + 10 )*pr[i]} ; k<j<i  =>  dp[j ...

  7. HDU1540 Tunnel Warfare(线段树区间维护&求最长连续区间)题解

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  8. codeforces 1097 Hello 2019

    又回来了.. A - Gennady and a Card Game 好像没什么可说的了. #include<bits/stdc++.h> using namespace std; cha ...

  9. Package.json 属性说明

    name - 包名. version - 包的版本号. description - 包的描述. entry pointer 项目入口文件 没有的直接回车跳过 test command: 测试命令 后面 ...

  10. 处理div 在IE6 IE7 IE8 不居中的问题

    具体处理方式如下:1 .html 顶部加入:DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...