栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【Linux】线程概念引入及编程实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Linux】线程概念引入及编程实现

线程的概念就是可以两个线程一起执行,比如一个程序中可以有2个while循环2个循环互不影响的执行着各自的程序。

1.如果创建Linux线程?

普通的C语言程序两个while循环:

#include "stdio.h"

void fun1()
{
        while(1)
        {
                printf("this is fun 1n");
                sleep(1);
        }
}
void fun2()
{
        while(1)
        {
                printf("this is fun 2n");
                sleep(1);
        }
}

int main()
{
        fun1();
        fun2();
        return 0;
}

线程的写法:

线程的编译运行

线程需要包含头文件#include "pthread.h"

pthread_t th1; //创建第一个线程

pthread_t th2;//创建第二个线程

#include "stdio.h"
#include "pthread.h"

void* fun1()
{
        while(1)
        {
                printf("this is fun 1n");
                sleep(1);
        }
}
void* fun2()
{
        while(1)
        {
                printf("this is fun 2n");
                sleep(1);
        }
}

int main()
{
        pthread_t th1;
        pthread_t th2;
        pthread_create(&th1,NULL ,fun1,NULL);
        pthread_create(&th2,NULL ,fun2,NULL);
        while(1);
        return 0;
}

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1012964.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号