
转自: https://feixiaoxing.blog.csdn.net/article/details/7240833
【 嵌牛导读 】本文将介绍linux下的C语言开发中的线程等待
【 嵌牛鼻子 】linux C语言 线程等待
【 嵌牛提问 】linux下的C语言开发中的线程等待是什么?
和多进程一样,多线程也有自己的等待函数。这个等待函数就是pthread_join函数。那么这个函数有什么用呢?我们其实可以用它来等待线程运行结束。
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
void func(void* args)
{
sleep(2)
printf("this is func!\n")
}
int main()
{
pthread_t pid
if(pthread_create(&pid, NULL, func, NULL))
{
return -1
}
pthread_join(pid, NULL)
printf("this is end of main!\n")
return 0
}
编写wait.c文件结束之后,我们就可以开始编译了。首先你需要输入gcc wait.c -o wait -lpthread,编译之后你就可以看到wait可执行文件,输入./wait即可。
[test@localhost thread]$ ./thread
this is func!
this is end of main!
这么高的悬赏,实例放后面。信号量(sem),如同进程一样,线程也可以通过信号量来实现通信,虽然是轻量级的。信号量函数的名字都以"sem_"打头。线程使用的基本信号量函数有四个。
信号量初始化。int sem_init (sem_t *sem , int pshared, unsigned int value)
这是对由sem指定的信号量进行初始化,设置好它的共享选项(linux 只支持为0,即表示它是当前进程的局部信号量),然后给它一个初始值VALUE。
等待信号量。给信号量减1,然后等待直到信号量的值大于0。
int sem_wait(sem_t *sem)
释放信号量。信号量值加1。并通知其他等待线程。
int sem_post(sem_t *sem)
销毁信号量。我们用完信号量后都它进行清理。归还占有的一切资源。
int sem_destroy(sem_t *sem) #include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!/n", __func__)return}
typedef struct _PrivInfo
{
sem_t s1
sem_t s2
time_t end_time
}PrivInfo
static void info_init (PrivInfo* thiz)
static void info_destroy (PrivInfo* thiz)
static void* pthread_func_1 (PrivInfo* thiz)
static void* pthread_func_2 (PrivInfo* thiz)
int main (int argc, char** argv)
{
pthread_t pt_1 = 0
pthread_t pt_2 = 0
int ret = 0
PrivInfo* thiz = NULL
thiz = (PrivInfo* )malloc (sizeof (PrivInfo))
if (thiz == NULL)
{
printf ("[%s]: Failed to malloc priv./n")
return -1
}
info_init (thiz)
ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz)
if (ret != 0)
{
perror ("pthread_1_create:")
}
ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz)
if (ret != 0)
{
perror ("pthread_2_create:")
}
pthread_join (pt_1, NULL)
pthread_join (pt_2, NULL)
info_destroy (thiz)
return 0
}
static void info_init (PrivInfo* thiz)
{
return_if_fail (thiz != NULL)
thiz->end_time = time(NULL) + 10
sem_init (&thiz->s1, 0, 1)
sem_init (&thiz->s2, 0, 0)
return
}
static void info_destroy (PrivInfo* thiz)
{
return_if_fail (thiz != NULL)
sem_destroy (&thiz->s1)
sem_destroy (&thiz->s2)
free (thiz)
thiz = NULL
return
}
static void* pthread_func_1 (PrivInfo* thiz)
{
return_if_fail(thiz != NULL)
while (time(NULL) < thiz->end_time)
{
sem_wait (&thiz->s2)
printf ("pthread1: pthread1 get the lock./n")
sem_post (&thiz->s1)
printf ("pthread1: pthread1 unlock/n")
sleep (1)
}
return
}
static void* pthread_func_2 (PrivInfo* thiz)
{
return_if_fail (thiz != NULL)
while (time (NULL) < thiz->end_time)
{
sem_wait (&thiz->s1)
printf ("pthread2: pthread2 get the unlock./n")
sem_post (&thiz->s2)
printf ("pthread2: pthread2 unlock./n")
sleep (1)
}
return
}
(1)Posix标准中有有名信号灯和无名信号灯之分,对于有名信号灯,可以用sem_open来创建,其prototype是:sem_t *sem_open(const char *name, int oflag)//打开已有的信号灯
sem_t *sem_open(const char *name, int oflag, mode_t mode, unsigned value)//一般是创建信号灯。
期中name是信号灯的名字, oflag是0, O_CREAT 或者 O_CREAT | O_EXCL, 如果指定O_CREAT, 那么mode和value对应创建该信号的模式和初始值。 如果指定了O_EXCL, 而且该信号灯已经在系统中存在,那调用会出错返回SEM_FAILED常量。 对于Linux内核来说,有名信号灯是很晚才加入内核中的,创建或是打开有名信号时候,应该指定”/semname“名字,对应的信号灯创建在/dev/shm目录下,名字是/dev/shm/sem.semname. BTW, 用gcc/g++编译实用信号灯功能的程序时候,应该引用librt库,(e.g., g++ -lrt sem.cpp). 关闭已打开的信号灯,用sem_close(sem_t *sem). 关闭信号灯并不意味着系统会删除它,要删除一个信号灯,需要调用sem_unlink(sem_t *sem)。 有名信号灯一般是为了进程之间同步实用的。 无名信号灯,一般是为一个进程内的不同线程之间同步使用的。 创建无名信号灯的方法如下:
sem_t sem
sem_init(&sem, int shared, unsigned int value)//初始化信号灯。
......
sem_destroy(&sem)//清除信号灯。
(2)信号灯的使用和状态。
信号灯一般用来描述不同线程所共享的公共资源的数量,每一个信号灯都有一个叫做信号量的非负整数与之相连;信号量一般代表公共资源的数目,比如空闲列表中的缓冲区数目,视频中读入帧的数目,等等。对于一个线程可以用sem_wait, sem_post函数来改变一个信号灯的信号量。
sem_wait(sem_t &sem)
sem_wait的语义如下:
{
while(信号量==0)
等待; //此处线程被挂起,等待其他线程调用sem_post唤醒之。
信号量减1;
}
注意:测试信号量是否为零,和减一的 *** 作是原子的,也就是说期间不会发生线程切换。
与sem_wait对应的调用是sem_post,语义如下:
{
信号量加1;
唤醒等待该信号量的线程;//调用sem_wait并等待的线程。
}
该 *** 作也是原子的。
信号灯的状态可以用sem_getvalue来查看。一般来说sem_wait和sem_post的调用不必在同一个线程内成对出现(象mutex那样,lock/unlock要配对出现)。 一般的情形是这样的,一个线程等待资源可用,调用sem_wait, 另外一个线程生成资源,然后调用sem_post,唤醒等待该资源的线程。因为信号灯所描述的是线程间公共资源,使用的时候一般和mutex一起使用,mutex保证访问公共资源的线程排他性,信号灯表示资源的可用性。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)