1.创建多线程
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
static void *my_thread_func (void *data)
{
while (1)
{
sleep(1);
}
}
int main(int argc, char **argv)
{
pthread_t tid;
int ret;
ret = pthread_create(&tid, NULL, my_thread_func, NULL);
if (ret)
{
printf("pthread_create err!\n");
return -1;
}
while (1)
{
sleep(1);
}
return 0;
}
程序后台执行,本终端查看 ps查看进程,只有1个,假设编译出来的程序名字叫pthread ps -T可以看到2个进程,结果一样,SPID不一样 cd /proc/进程ID cd task
#killall pthread可以杀死全部线程 ps -AT查看全部进程
2.CPU占用率问题
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
static char g_buf[1000];
static int g_hasData = 0;
static void *my_thread_func (void *data)
{
while (1)
{
while (g_hasData == 0);
printf("recv: %s\n", g_buf);
g_hasData = 0;
}
return NULL;
}
int main(int argc, char **argv)
{
pthread_t tid;
int ret;
ret = pthread_create(&tid, NULL, my_thread_func, NULL);
if (ret)
{
printf("pthread_create err!\n");
return -1;
}
while (1)
{
fgets(g_buf, 1000, stdin);
g_hasData = 1;
}
return 0;
}
这个cpu占有率非常高
3.线程同步
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <semaphore.h>
static char g_buf[1000];
static sem_t g_sem;
static void *my_thread_func (void *data)
{
while (1)
{
sem_wait(&g_sem);
printf("recv: %s\n", g_buf);
}
return NULL;
}
int main(int argc, char **argv)
{
pthread_t tid;
int ret;
sem_init(&g_sem, 0, 0);
ret = pthread_create(&tid, NULL, my_thread_func, NULL);
if (ret)
{
printf("pthread_create err!\n");
return -1;
}
while (1)
{
fgets(g_buf, 1000, stdin);
sem_post(&g_sem);
}
return 0;
}
降低了cpu占有率问题,假如两个线程同时访问这个全局数组,可能导致数据乱了
4.互斥访问
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <semaphore.h>
#include <string.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);
}
return NULL;
}
int main(int argc, char **argv)
{
pthread_t tid;
int ret;
char buf[1000];
sem_init(&g_sem, 0, 0);
ret = pthread_create(&tid, NULL, my_thread_func, NULL);
if (ret)
{
printf("pthread_create err!\n");
return -1;
}
while (1)
{
fgets(buf, 1000, stdin);
pthread_mutex_lock(&g_tMutex);
memcpy(g_buf, buf, 1000);
pthread_mutex_unlock(&g_tMutex);
sem_post(&g_sem);
}
return 0;
}
互斥访问,并且增加了一层缓冲区。视频里面主线程会一直执行,子线程可能没那么快执行到
5.第二种同步与互斥,条件变量+互斥
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <semaphore.h>
#include <string.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);
}
return NULL;
}
int main(int argc, char **argv)
{
pthread_t tid;
int ret;
char buf[1000];
ret = pthread_create(&tid, NULL, my_thread_func, NULL);
if (ret)
{
printf("pthread_create err!\n");
return -1;
}
while (1)
{
fgets(buf, 1000, stdin);
pthread_mutex_lock(&g_tMutex);
memcpy(g_buf, buf, 1000);
pthread_cond_signal(&g_tConVar);
pthread_mutex_unlock(&g_tMutex);
}
return 0;
}
|