📄 threadtest.c
字号:
/*threadtest.c*/
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>
/*声明线程运行服务程序*/
static void pthread_func_1 (void);
static void pthread_func_2 (void);
int main (void)
{
/*线程的标识符*/
pthread_t pt_1 = 0;
pthread_t pt_2 = 0;
int ret = 0;
/*分别创建线程1、2*/
ret = pthread_create (&pt_1, //线程标识符指针
NULL, //默认属性
(void *)pthread_func_1,//运行函数
NULL); //无参数
if (ret != 0)
{
perror ("pthread_1_create");
}
ret = pthread_create (&pt_2, //线程标识符指针
NULL, //默认属性
(void *)pthread_func_2, //运行函数
NULL); //无参数
if (ret != 0)
{
perror ("pthread_2_create");
}
/*等待线程1、2的结束*/
pthread_join (pt_1, NULL);
pthread_join (pt_2, NULL);
printf ("main programme exit!\n");
return 0;
}
/*线程1的服务程序*/
static void pthread_func_1 (void)
{
int i = 0;
for (; i < 6; i++)
{
printf ("This is pthread1!\n");
/*i==2时退出,即循环3次*/
if (i == 2)
{
pthread_exit (0);
}
sleep (1);
}
}
/*线程2的服务程序*/
static void pthread_func_2 (void)
{
int i = 0;
for (; i < 3; i++)
{
printf ("This is pthread2!\n");
}
pthread_exit (0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -