【嵌入式应用开发基础之多线程编程】

代码

p_thread1-创建线程

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>


static void *my_thread_func (void *data)
{
	while(1)
	{
		sleep(1);
	}
}

int main(int argc, char** argv)
{
	pthread_t tid;
	int iRet;
	/* 1. 创建"接收线程" */
	iRet = pthread_create(&tid, NULL, my_thread_func, NULL);
	if (iRet)
	{
		printf("pthread create err\n");
		return -1;
	}

	/* 2. 主线程读取标准输入,发给"接收线程" */
	while(1)
	{
		sleep(1);
	}
	return 0;
	
}

p_thread2-自实现同步操作

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>


static char g_buf[1000];
static int g_hasData = 0;
static void *my_thread_func (void *data)
{
	while(1)
	{
		/* 等待通知 */
#if 0
		if (g_hasData == 1)
		{
			printf("recv: %s\n", g_buf);
		}
		/* 打印 */
#else
		while(g_hasData == 0);
		printf("recv: %s\n", g_buf);
#endif
		g_hasData = 0;
	}
}

int main(int argc, char** argv)
{
	pthread_t tid;
	int iRet;
	/* 1. 创建"接收线程" */
	iRet = pthread_create(&tid, NULL, my_thread_func, NULL);
	if (iRet)
	{
		printf("pthread create err\n");
		return -1;
	}

	/* 2. 主线程读取标准输入,发给"接收线程" */
	while(1)
	{
		fgets(g_buf, 1000, stdin);
		
		/* 通知接收线程 */
		g_hasData = 1;
		
		
	}
	return 0;
}
  • 在实际测试过程中,接收线程中用if()代替while()进行查询,发现输入并没有打印在屏幕上,原因应该是g_buf一直在被置零,两个线程都可以对g_hasData进行操作,会发生争抢,互相干扰,可以用互斥锁解决。
  • 使用while()时,top命令查看资源占用情况,CPU资源占用率将近为100%,因为while()一直在执行,所以这里需要一个没检测到就休眠的信号量
    在这里插入图片描述

p_thread3-信号量同步

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <semaphore.h>


static char g_buf[1000];
static int g_hasData = 0;
static sem_t g_sem;

static void *my_thread_func (void *data)
{
	while(1)
	{
		/* 等待信号量 */
		sem_wait(&g_sem);
		/* 打印 */
		printf("recv: %s\n", g_buf);
		g_hasData = 0;
	}
}

int main(int argc, char** argv)
{
	pthread_t tid;
	int iRet;
	/* 1. 创建"接收线程" */
	iRet = pthread_create(&tid, NULL, my_thread_func, NULL);
	if (iRet)
	{
		printf("pthread create err\n");
		return -1;
	}
	
	sem_init(&g_sem,0,0);

	/* 2. 主线程读取标准输入,发给"接收线程" */
	while(1)
	{
		if(fgets(g_buf, 1000, stdin) != NULL)
		{
			/* 释放信号量 */
			sem_post(&g_sem);
		}
	}
	return 0;
	
}

三个函数 sem_init();sem_post();sem_wait(),让top中找不到线程资源

  • 可能存在的问题
    在打印buffer的过程中,如果又输入了新的数据,则很有可能存在新旧数据重叠问题,故需要互斥量来保证不同线程的互相干扰问题

p_thread4-信号量同步、互斥锁

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <semaphore.h>


static char g_buf[1000];
static sem_t g_sem;
static pthread_mutex_t g_tMutex  = PTHREAD_MUTEX_INITIALIZER;


static void *my_thread_func (void *data)
{
	while(1)
	{
		/* 等待信号量 */
		sem_wait(&g_sem);
		/* 打印 */
		pthread_mutex_lock(&g_tMutex);
		printf("recv: %s\n", g_buf);
		pthread_mutex_unlock(&g_tMutex);
	}
}

int main(int argc, char** argv)
{
	pthread_t tid;
	int iRet;
	/* 1. 创建"接收线程" */
	iRet = pthread_create(&tid, NULL, my_thread_func, NULL);
	if (iRet)
	{
		printf("pthread create err\n");
		return -1;
	}
	
	sem_init(&g_sem,0,0);

	/* 2. 主线程读取标准输入,发给"接收线程" */
	while(1)
	{
		if(fgets(g_buf, 1000, stdin) != NULL)
		{
			pthread_mutex_lock(&g_tMutex);
			/* 释放信号量 */
			sem_post(&g_sem);
			pthread_mutex_unlock(&g_tMutex);
		}
	}
	return 0;
	
}
  • 互斥锁占用时间问题
    在测试过程中,如果写成这个形式,互斥锁会一直被主进程获得,信号量刚刚释放,还没有被接收线程获得就又一次进入while循环,互斥锁又被主进程获得,打印不出信息
	while(1)
	{
		pthread_mutex_lock(&g_tMutex);
		fgets(g_buf, 1000, stdin) != NULL
		pthread_mutex_unlock(&g_tMutex);
		/* 释放信号量 */
		sem_post(&g_sem);
	}

更改为以下方式

	while(1)
	{
		if(fgets(g_buf, 1000, stdin) != NULL)
		{
			pthread_mutex_lock(&g_tMutex);
			/* 释放信号量 */
			sem_post(&g_sem);
			pthread_mutex_unlock(&g_tMutex);
		}
	}

避免主线程占用互斥锁的时间过长

p_thread5-条件同步、互斥锁

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <semaphore.h>


static char g_buf[1000];
static pthread_mutex_t g_tMutex  = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t  g_tConVar = PTHREAD_COND_INITIALIZER;



static void *my_thread_func (void *data)
{
	while(1)
	{
		/* 获取互斥锁 */
		pthread_mutex_lock(&g_tMutex);
		/* 等待条件成立,条件成立执行接下来的代码,条件不成立释放互斥锁,继续等待 */
		pthread_cond_wait(&g_tConVar, &g_tMutex);	
		/* 打印 */
		printf("recv: %s\n", g_buf);
		pthread_mutex_unlock(&g_tMutex);
	}
}

int main(int argc, char** argv)
{
	pthread_t tid;
	int iRet;
	/* 1. 创建"接收线程" */
	iRet = pthread_create(&tid, NULL, my_thread_func, NULL);
	if (iRet)
	{
		printf("pthread create err\n");
		return -1;
	}
	

	/* 2. 主线程读取标准输入,发给"接收线程" */
	while(1)
	{
		if(fgets(g_buf, 1000, stdin) != NULL)
		{
			pthread_mutex_lock(&g_tMutex);
			/* 通知接收线程 */
			pthread_cond_signal(&g_tConVar);
			/* 释放信号量 */
			pthread_mutex_unlock(&g_tMutex);
		}
	}
	return 0;
}
/************************************************ * * The classic producer-consumer example. * Illustrates mutexes and conditions. * by Zou jian guo * 2003-12-22 * *************************************************/ #include #include #include #include "pthread.h" #define BUFFER_SIZE 16 /* Circular buffer of integers. */ struct prodcons { int buffer[BUFFER_SIZE]; /* the actual data */ pthread_mutex_t lock; /* mutex ensuring exclusive access to buffer */ int readpos, writepos; /* positions for reading and writing */ pthread_cond_t notempty; /* signaled when buffer is not empty */ pthread_cond_t notfull; /* signaled when buffer is not full */ }; /*--------------------------------------------------------*/ /* Initialize a buffer */ void init(struct prodcons * b) { pthread_mutex_init(&b->lock, NULL); pthread_cond_init(&b->notempty, NULL); pthread_cond_init(&b->notfull, NULL); b->readpos = 0; b->writepos = 0; } /*--------------------------------------------------------*/ /* Store an integer in the buffer */ void put(struct prodcons * b, int data) { pthread_mutex_lock(&b->lock); /* Wait until buffer is not full */ while ((b->writepos + 1) % BUFFER_SIZE == b->readpos) { printf("wait for not full\n"); pthread_cond_wait(&b->notfull, &b->lock); } /* Write the data and advance write pointer */ b->buffer[b->writepos] = data; b->writepos++; if (b->writepos >= BUFFER_SIZE) b->writepos = 0; /* Signal that the buffer is now not empty */ pthread_cond_signal(&b->notempty); pthread_mutex_unlock(&b->lock); } /*--------------------------------------------------------*/ /* Read and remove an integer from the buffer */ int get(struct prodcons * b) { int data; pthread_mutex_lock(&b->lock); /* Wait until buffer is not empty */ while (b->writepos == b->readpos) { printf("wait for not empty\n"); pthread_cond_wait(&b->notempty, &b->
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值