sem_init
SEM_INIT(3) Linux Programmer's Manual SEM_INIT(3)
NAME
sem_init - initialize an unnamed semaphore
SYNOPSIS
#include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned int value);
Link with -pthread.
sem_wait
NAME
sem_wait, sem_timedwait, sem_trywait - lock a semaphore
SYNOPSIS
#include <semaphore.h>
int sem_wait(sem_t *sem);
int sem_trywait(sem_t *sem);
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
Link with -pthread.
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
sem_timedwait(): _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
struct timespec {
time_t tv_sec; /* Seconds */
long tv_nsec; /* Nanoseconds [0 .. 999999999] */
};
sem_post 函数
NAME
sem_post - unlock a semaphore
SYNOPSIS
#include <semaphore.h>
int sem_post(sem_t *sem);
Link with -pthread.
综合测试代码
测试代码:
验证项目:
1 signal是进程级别的
2 sem_t和发送和等待
3 线程传递结构体参数
4 发送一个signal
5 线程的分离
#include <stdio.h>
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
#include <signal.h>
#include <map>
#define DEBUG_INFO(format, ...) printf("%s:%d -- " format "\n", __func__, __LINE__,##__VA_ARGS__)
typedef void (*sighandler)(int signo);
std::map<pthread_t ,std::string> thread_map;
void sig_func(int signo) {
switch(signo) {
case SIGUSR1:
DEBUG_INFO("SIGUSR1 signal %u,%lu",getpid(),pthread_self());
std::cout << __func__ << " " << thread_map[pthread_self()] << std::endl;
break;
default:
DEBUG_INFO("unkonwn signo = %d\n", signo);
break;
}
}
struct mystruct{
pthread_t thread_id;
sem_t sem;
char buf[1024];
int fd;
int socket;
int arg1;
int arg2;
};
void* func(void *arg){
struct mystruct *pms = (struct mystruct*)arg;
pthread_detach(pthread_self());
signal(SIGUSR1,sig_func);
DEBUG_INFO("pthread_self = %lu\n", pthread_self());
DEBUG_INFO("wait a semaphore");
sem_wait(&pms->sem);
std::cout << thread_map[pthread_self()] << std::endl;
DEBUG_INFO("get a semaphore thread_id = %lu",pms->thread_id);
DEBUG_INFO("thread pid = %u",getpid());
while(1){
//process(fd)
//process(socket)
usleep(1);
}
}
int main(int argc, char **argv){
std::cout << "Usage: " << argv[0] << std::endl;
DEBUG_INFO("main pid = %u",getpid());
DEBUG_INFO("%ld",sizeof(pthread_t));
DEBUG_INFO("main thread = %lu\n", pthread_self());
thread_map[pthread_self()] = std::string("main thread");
std::cout << __func__ << " " << thread_map[pthread_self()] << std::endl;
for(int i = 0; i < 3;i++){
struct mystruct *pms = (struct mystruct*)malloc(sizeof(struct mystruct));
if(pms == NULL){
perror("malloc");
exit(-1);
}
sem_init(&pms->sem,0,0);
pthread_t t;
int ret = pthread_create(&t,NULL,func,pms);
if(ret != 0){
perror("pthread_create");
exit(1);
}
thread_map[pthread_self()] = std::string("child thread:") + std::to_string(i);
pms->thread_id = t;
sleep(1);
sem_post(&pms->sem);
}
kill(getpid(),SIGUSR1);
kill(getpid(),SIGUSR1);
kill(getpid(),SIGUSR1);
kill(getpid(),SIGUSR1);
kill(getpid(),SIGUSR1);
while(1){
sleep(1);
}
DEBUG_INFO("bye bye");
return 0;
}
执行结果:
main:55 -- main pid = 24855
main:56 -- 8
main:57 -- main thread = 139649254037312
main main thread
func:40 -- pthread_self = 139649236711168
func:41 -- wait a semaphore
func:44 -- get a semaphore thread_id = 139649236711168
func:45 -- thread pid = 24855
func:40 -- pthread_self = 139649228318464
func:41 -- wait a semaphore
func:44 -- get a semaphore thread_id = 139649228318464
func:45 -- thread pid = 24855
func:40 -- pthread_self = 139649219925760
func:41 -- wait a semaphore
sig_func:17 -- SIGUSR1 signal 24855,139649254037312
sig_func child thread:2
sig_func:17 -- SIGUSR1 signal 24855,139649254037312
sig_func child thread:2
sig_func:17 -- SIGUSR1 signal 24855,139649254037312
func:44 -- get a semaphore thread_id = 139649219925760
func:45 -- thread pid = 24855
sig_func child thread:2
sig_func:17 -- SIGUSR1 signal 24855,139649254037312
sig_func child thread:2
sig_func:17 -- SIGUSR1 signal 24855,139649254037312
sig_func child thread:2
小结
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)