linux线程控制函数,Linux系统编程(16)——线程控制相关函数-创建进程、查看线程信息...

1.创建进程

函数:

pthread_create(pthread_t *thread, const pthread_attr_t *att, void *(*strart_routine)(void*), void *arg);

参数:

pthread_t *thread:线程ID的地址,新线程的id

const pthread_attr_t *att:是个结构体,设置属性,一般用不到,NULL就好了

void *(*strart_routine)(void*):函数指针,就相当于新线程的入口函数,指定了新线程执行那个代码

void *arg:上个函数指针,入口函数的参数就是这个。

//创建线程

#include #include #include//头文件

#include //sleep头文件

void* ThreadEntry(void* arg) { //新线程入口,参数

(void) arg;

while (1) {

printf("In ThreadEntry\n");

Sleep(1);

}

}

int main() {

//创建线程函数:pthread_create();

//pthread_create(pthread_t *thread, const pthread_attr_t *att, void *(*strart_routine)(void*), void *arg);

pthread_t tid;

pthread_create(&tid, NULL, ThreadEntry, &arg); //创建一个新线程,新线程id tid

while (1) { //主线程

printf("In Main Thread\n");

Sleep(1);

}

system("pause");

return 0;

}

//Makefile文件中

teat:teat.c

gcc $^ -o $@ -lpthread

注意:Makefile 文件中要gcc 那一行中要加上-lpthread(表示要从pthread库中连接 pthread_create() 函数)

gcc -l表示连接一个库,直接写 库名就好(不包含 lib 和 .a/.so) -lpthread

另外:线程之间也是抢占式实现的~~~谁先执行不一定~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值