线程控制原语
编译:gcc pthread.c -o ph -pthread
pthread_slef() = getpid()
pthread_create() = fork()
pthread_cancel() = kill
pthread_join() = wait()
pthread_exit()
pthread_detach()
pthread_equal()
创建子线程
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void *thrd_func(void *arg)
{
printf("In thread: thread id = %lu, pid = %u\n", pthread_self(), getpid());
return NULL;
}
int main()
{
pthread_t tid;
int res;
printf("In main 1: thread id = %lu, pid = %u\n", pthread_self(), getpid());
res = pthread_create(&tid, NULL, thrd_func, NULL);
if (res != 0)
{
printf("create thread error!");
exit(1);
}
sleep(1);
printf("In main 2: thread id = %lu, pid = %u\n", pthread_self(), getpid());
return 0;
}
循环创建多个子线程
void * 在32位系统中和int占相同的空间,4字节。
如果是64位系统中,void *是8字节,但是大转小是高位补零和高位截取0,不影响数据效果。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
void *thrd_func(void *arg)
{
int i = (int)arg;
sleep(i);
printf("%dth thread: thread id = %lu, pid = %u\n", i + 1, pthread_self(), getpid());
return NULL;
}
int main()
{
pthread_t tid;
int res, i;
for (i = 0; i < 5; i++)
{
res = pthread_create(&tid, NULL, thrd_func, (void *)i);
if (res != 0)
{
fprintf(stderr, "create thread error: %s \n", strerror(res));
exit(1);
}
}
sleep(i);
return 0;
}
线程共享全局变量
进程不共享全局变量
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
int var = 100;
void *thrd_func(void *arg)
{
var = 200;
printf("In thread: var = %d\n", var);
return NULL;
}
int main()
{
pthread_t tid;
int i, res;
printf("In main: before create thread var = %d\n", var);
res = pthread_create(&tid, NULL, thrd_func, (void *)i);
if (res != 0)
{
fprintf(stderr, "create thread error: %s \n", strerror(res));
exit(1);
}
sleep(1);
printf("In main: var = %d\n", var);
return 0;
}
thread_exit函数
单个线程退出,进程调用thread_create后,也变成一个线程。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void *thrd_func(void *arg)
{
printf("In thread: thread id = %lu, pid = %u\n", pthread_self(), getpid());
return NULL;
}
int main()
{
pthread_t tid;
int res;
printf("In main 1: thread id = %lu, pid = %u\n", pthread_self(), getpid());
res = pthread_create(&tid, NULL, thrd_func, NULL);
if (res != 0)
{
printf("create thread error!");
exit(1);
}
printf("In main 2: thread id = %lu, pid = %u\n", pthread_self(), getpid());
pthread_exit((void *)4);
return 0;
}
thread_join回收子线程
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void *thrd_func(void *arg)
{
printf("In thread: thread id = %lu, pid = %u\n", pthread_self(), getpid());
pthread_exit((void *)100);
}
int main()
{
pthread_t tid;
int res;
res = pthread_create(&tid, NULL, thrd_func, NULL);
if (res != 0)
{
printf("create thread error!");
exit(1);
}
int *retrival;
pthread_join(tid, (void **)&retrival);
printf("---------%d-----\n", (int)retrival);
pthread_exit((void *)4);
return 0;
}
回收多个子线程
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
int var = 100; //全局变量 线程共享
void *thrd_func(void *arg)
{
int i = (int)arg;
sleep(i);
if (i < 1)
{
var = 200;
printf("I'm %dth thread: thread id = %lu, var = %d\n", i + 1, pthread_self(), var);
return (void *)var;
}
else if (i < 3)
{
var = 300;
printf("I'm %dth thread: thread id = %lu, var = %d\n", i + 1, pthread_self(), var);
pthread_exit((void *)var);
}
else
{
var = 400;
printf("I'm %dth thread: thread id = %lu, var = %d\n", i + 1, pthread_self(), var);
pthread_exit((void *)var);
}
return NULL;
}
int main()
{
pthread_t tid[5];
int res, i;
int *ret[5]; //这里必须是指针数组
printf("I'm main thread tid = %lu, var = %d\n", pthread_self(), var);
for (i = 0; i < 5; i++) //创建多个子线程
{
res = pthread_create(&tid[i], NULL, thrd_func, (void *)i);
if (res != 0)
{
fprintf(stderr, "create thread error: %s \n", strerror(res));
exit(1);
}
}
//回收多个子线程
for (i = 0; i < 5; i++)
{
pthread_join(tid[i], (void **)&ret[i]); //这里要取地址
printf("-----%d's ret = %d----\n", i + 1, (int)ret[i]);
}
printf("I'm main thread tid = %lu, var = %d\n", pthread_self(), var);
return 0;
}
pthread_detach线程分离
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
void *thrd_func(void *arg)
{
int i = (int)arg;
sleep(i);
printf("I'm %dth thread, tid = %lu pid = %u\n", i + 1, pthread_self(), getpid());
pthread_exit((void *)22);
}
int main()
{
pthread_t tid[5];
int res, i;
for (i = 0; i < 5; i++) //创建多个子线程
{
res = pthread_create(&tid[i], NULL, thrd_func, (void *)i);
pthread_detach(tid[i]);
}
sleep(5);
return 0;
}
pthread_cancel杀死线程
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
int i = 0;
void *thrd_func1(void *arg)
{
printf("I'm %dth thread, tid = %lu pid = %u\n", ++i, pthread_self(), getpid());
while (1)
{
int a = 3;
int b = a;
pthread_testcancel(); //自己添加取消点
}
return (void *)666;
}
void *thrd_func(void *arg)
{
printf("I'm %dth thread, tid = %lu pid = %u\n", ++i, pthread_self(), getpid());
pthread_exit((void *)22);
}
int main()
{
pthread_t tid;
void *ret = NULL;
//线程1
pthread_create(&tid, NULL, thrd_func, NULL);
pthread_join(tid, &ret); //void**
printf("thread 1 exit code = %d\n", (int)ret);
//线程2 死循环 需要手动杀死
pthread_create(&tid, NULL, thrd_func1, NULL);
sleep(3);
pthread_cancel(tid); //杀死线程 必须到达取消点
pthread_join(tid, &ret);
printf("thread 1 exit code = %d\n", (int)ret);
return 0;
}
pthread_equal()
判断两个线程ID是否相等