线程
来源:互联网 发布:php mvc项目 编辑:程序博客网 时间:2024/05/29 09:23
1.什么是线程?
定义:在一个程序中多个执行路线就是线程。线程是一个进程内部的一个控制序列。所有进程都有一个执行线程。
区别:当进程执行fork调用时,将创建出该进程的一个副本,新进程由自己的PID,时间调度也是独立的,执行几乎完全独立于父进程。
当创建一个新线程时,新线程拥有自己的栈,但它与创建者共享全局变量,文件描述符,信号处理函数和当前目录状态。
2.第一个线程程序。
创建线程
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
成功返回0失败返回错误代码
终止线程
#include <pthread.h>void pthread_exit(void *retval);终止线程并且返回某个对象的指针。
#include <pthread.h>int pthread_join(pthread_t thread, void **retval);
retval返回线程的返回值。 成功返回0失败返回错误代码
thread1.c:两个线程共享变量,主线程获取另外一个线程的返回值$ vi thread1.c
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
#include<unistd.h>
char message[] = "hello world";
void *thread_function(void *arg)
{
printf("thread_function massage : %s\n",(char*)arg);
sleep(1);
strcpy(message,"OK OK OK");
pthread_exit("thread_function gameover");
}
int main()
{
pthread_t thread;
void *thread_result;
int res = pthread_create(&thread,NULL,thread_function,(void *)message);
if(res != 0)
{
perror("thread create error");
exit(EXIT_FAILURE);
}
res = pthread_join(thread,&thread_result);
if(res != 0)
{
perror("thread_join error");
exit(EXIT_FAILURE);
}
printf("thread was runed\n");
printf("thread_jion returned is %s\n",(char *)thread_result);
printf("now massege is %s\n",message);
exit(EXIT_SUCCESS);
}
$ gcc -o thread1 thread1.c -lpthread
$ ./thread1
thread_function massage : hello world
thread was runed
thread_jion returned is thread_function gameover
now massege is OK OK OK
thread2.c:两个线程同时执行(在两个线程中使用轮询技术)
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
#include<unistd.h>
int run_now = 1;
void *thread_function(void *arg)
{
int count1 = 0;
while(count1<10)
{
if(run_now == 2)
{
printf("thread_function running\n");
run_now = 1;
}
else
{
sleep(1);
}
count1++;
}
pthread_exit("thread_function gameover");
}
int main()
{
pthread_t thread;
void *thread_result;
int res =pthread_create(&thread,NULL,thread_function,(void *)0);
if(res != 0)
{
perror("thread create error");
exit(EXIT_FAILURE);
}
int count2 = 0;
while(count2<10)
{
if(run_now == 1)
{
printf("main running\n");
run_now = 2;
}
else
{
sleep(1);
}
count2++;
}
res = pthread_join(thread,&thread_result);
if(res != 0)
{
perror("thread_join error");
exit(EXIT_FAILURE);
}
printf("thread_jion returned is %s\n",(char *)thread_result);
exit(EXIT_SUCCESS);
}
$ gcc -o thread2 thread2.c -lpthread
$ ./thread2
main running
thread_function running
main running
thread_function running
main running
thread_function running
main running
thread_function running
main running
thread_function running
thread_jion returned is thread_function gameover
thread3.c:一个线程信号量(用信号量进行同步,主线程写,另外一个线程读字符串的长度)
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
#include<unistd.h>
#include<semaphore.h>
#define BUFF_SIZE 1024
char Buff[BUFF_SIZE];
sem_t sem;
void *thread_function(void *arg)
{
while(strncmp(Buff,"end", 3) != 0)
{
sem_wait(&sem);
printf("You input %d characters\n",strlen(Buff)-1);
}
pthread_exit("thread_function gameover");
}
int main()
{
pthread_t thread;
void *thread_result;
int res = sem_init(&sem,0,0);
if(res != 0)
{
perror("sem_init error");
exit(EXIT_FAILURE);
}
res = pthread_create(&thread,NULL,thread_function,(void *)0);
if(res != 0)
{
perror("thread create error");
exit(EXIT_FAILURE);
}
printf("input end to finish\n");
while(strncmp(Buff,"end",3) != 0)
{
fgets(Buff,BUFF_SIZE,stdin);
sem_post(&sem);
}
res = pthread_join(thread,&thread_result);
if(res != 0)
{
perror("thread_join error");
exit(EXIT_FAILURE);
}
printf("thread was runed\n");
printf("thread_jion returned is %s\n",(char *)thread_result);
sem_destroy(&sem);
exit(EXIT_SUCCESS);
}
$ gcc -o thread3 thread3.c -lpthread
$ ./thread3
input end to finish
ni
You input 2 characters
hao
You input 3 characters
meimeida
You input 8 characters
woaini
You input 6 characters
dniejdiejd
You input 10 characters
wo aini chentong
You input 16 characters
end
You input 3 characters
thread was runed
thread_jion returned is thread_function gameover
thread4.c:用互斥量进行同步(一个线程输入数据,一个线程计算数据的大小)
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
#include<unistd.h>
#define BUFF_SIZE 1024
int time_to_exit = 1;
char buff[BUFF_SIZE];
pthread_mutex_t mutex;
void *thread_function(void *arg)
{
sleep(1);
pthread_mutex_lock(&mutex);
while(strncmp(buff,"end",3) != 0)
{
printf("You input %d charcters\n",strlen(buff)-1);
buff[0] = '\0';
pthread_mutex_unlock(&mutex);
sleep(1);
pthread_mutex_lock(&mutex);
while(buff[0] == '\0')
{
pthread_mutex_unlock(&mutex);
sleep(1);
pthread_mutex_lock(&mutex);
}
}
time_to_exit = 0;
buff[0] ='\0';
pthread_mutex_unlock(&mutex);
pthread_exit("thread_function gameover");
}
int main()
{
pthread_t thread;
void *thread_result;
int res = pthread_mutex_init(&mutex,NULL);
if(res != 0)
{
perror("pthread_mutex_init error");
exit(EXIT_FAILURE);
}
res = pthread_create(&thread,NULL,thread_function,(void *)0);
if(res != 0)
{
perror("thread create error");
exit(EXIT_FAILURE);
}
pthread_mutex_lock(&mutex);
while(time_to_exit)
{
fgets(buff,BUFF_SIZE,stdin);
pthread_mutex_unlock(&mutex);
while(1)
{
pthread_mutex_lock(&mutex);
if(buff[0] != 0)
{
pthread_mutex_unlock(&mutex);
sleep(1);
}
else
{
break;
}
}
}
pthread_mutex_unlock(&mutex);
printf("thread is finished...\n");
res = pthread_join(thread,&thread_result);
if(res != 0)
{
printf("ppthread_join() error\n");
exit(EXIT_FAILURE);
}
pthread_mutex_destroy(&mutex);
printf("thread_jion returned is %s\n",(char *)thread_result);
exit(EXIT_SUCCESS);
}
阅读全文0 0
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- 线程
- app拨测
- Android应用线程池最大线程数量
- 绘制圆形探照灯
- 文件的分割(split)与合并(cat)
- Kotlin版RecyclerView加item点击处理
- 线程
- Spark SQL filter not contains
- 总体性能——加载模块性能
- java 合并两个有序单链表
- [论文学习]Convolutional matrix factorization for document context-aware recommendation
- WebBench源码剖析(上)
- MFC中的DC,CDC和HDC
- HDU 3001 Travelling
- EOJ----连续正整数之和
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
万向集团招聘
十字轴万向联轴器
万向罩
伸缩万向节
万向节联轴器
十字万向节
十字轴万向节结构图
万向节传动轴
汽车万向节
十字轴万向节
万向节原理
万向节的作用
万向节怎么拆
不等速万向节
拖拉机万向节
万向节十字轴
双联式万向节
汽车传动轴万向节
球叉式万向节
万向节轴承型号
传动轴万向节图片
方向机万向节
万向节拆装
球笼式万向节图片
万向节平衡机
球万向节
万向节球
十字轴万向节拆装
万向节防尘罩
万向节的类型
万向节批发
yw万向节
bj212万向节
十字轴万向节联轴器
等速万向节的作用
球 万向节
万向节类型
多轴器
靠背轮
万向滚珠
平板万向轮