六十六. 线程的概念
所以线程之间通信,不可以用局部变量(因为局部变量在栈里)
这里的命令可以知道,各部分大小
[root@VM_0_15_centos ~]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7283
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 100001
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 7283
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
六十七. pthread_create线程创建函数
arg是线程处理函数的参数的内容
写一个pthread_create.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h> //这里是线程对应的头文件
void* myfunc(void* arg)
{
printf("child pthread id: %lu\n", pthread_self());
return NULL;
}
int main(int argc, const char* argv[])
{
// 创建子线程
pthread_t thid;
// 返回错误号
int ret = pthread_create(&thid, NULL, myfunc, NULL);
if(ret != 0)
{
printf("error number: %d\n", ret);
// 根据错误号打印错误信息
printf("error information: %s\n", strerror(ret));
}
printf("parent pthread id: %lu\n", pthread_self());
//这里sleep是因为,父线程和子线程也会竞争cpu。
//如果父线程先结束,那么则整个程序释放,子线程来不及执行,就结束了
sleep(1);
return 0;
}
运行一下
[root@VM_0_15_centos 8Day]# gcc pthread_create.c -lpthread
[root@VM_0_15_centos 8Day]# ls
a.out process_r.c pthread_create.c pthread_uncle.c
deamon.c process_work.c pthread_exit.c setsid.c
loop_pthread_create.c pthread_attr.c pthread_join.c
[root@VM_0_15_centos 8Day]# ./a.out
parent pthread id: 140423590418240
child pthread id: 140423582037760
六十八. 循环创建多个子线程和注意事项
这里写一个loop_pthread_create.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h>
void* myfunc(void* arg)
{
// int num = *(int*)arg;
int num = (int)arg;
printf("%dth child pthread id: %lu\n", num, pthread_self());
return NULL;
}
int main(int argc, const char* argv[])
{
// 创建子线程
pthread_t thid[5];
// 返回错误号
for(int i=0; i<5; ++i)
{
//int ret = pthread_create(&thid[i], NULL, myfunc, (void*)&i);
int ret = pthread_create(&thid[i], NULL, myfunc, (void*)i);
if(ret != 0)
{
printf("error number: %d\n", ret);
// 根据错误号打印错误信息
printf("error information: %s\n", strerror(ret));
}
}
printf("parent pthread id: %lu\n", pthread_self());
sleep(1);
return 0;
}
如果程序里,在pthread_create那里第4个参数,是(void*)&i,那么将会导致问题,原因是
所以那里需要改为传值。
运行结果
六十九. 复习
七十. 线程函数打印错误信息
这里写一个pthread_create.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h> //这里是线程对应的头文件
void* myfunc(void* arg)
{
//取数据
int num = *(int*)arg;
//打印子进程的id
printf("child pthread id: %lu\n", pthread_self());
return NULL;
}
int main(int argc, const char* argv[])
{
// 创建子线程
//线程ID变量
int ret = pthread_create(&thid[i], NULL, myfunc, (void*)&i);
if(ret != 0)
{
printf("error number: %d\n",ret);
//打印错误信息
printf("%s\n",strerror(ret));
}
printf("parent pthread id: %lu\n", pthread_self());
//这里sleep是因为,父线程和子线程也会竞争cpu。
//如果父线程先结束,那么则整个程序释放,子线程来不及执行,就结束了
sleep(1);
return 0;
}