https://blog.csdn.net/androidbbc/article/details/51479682
https://www.cnblogs.com/lang5230/p/5686851.html
https://www.cnblogs.com/biyeymyhjob/archive/2012/10/11/2720377.html
https://blog.csdn.net/special00/article/details/52279564
pthread_create 创建一个线程
pthread_self 找出自己的线程ID
pthread_equal 测试2个线程ID是否相等
pthread_detach 设置线程以释放资源,设置分离属性,非阻塞设置
pthread_join 阻塞等待一个线程
pthread_cancel 终止另一个线程
pthread_exit 退出线程,而不退出进程
pthread_kill 向线程发送一个信号
pthread_mutex_trylock
非阻塞
会尝试加锁,如果该互斥锁已经锁定,则返回一个不为0
的错误值,如果该互斥锁没有锁定,则返回0,表示尝试加锁成功
static pthread_cond_t cond_download_img = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t mutex_download_img = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_init
pthread_cond_wait(&cond_download_img, &mutex_download_img);
pthread_cond_signal(&cond_download_img);
pthread_cond_destroy(&cond_download_img);
pthread_mutex_t f_lock;
pthread_mutex_init(&f_lock, NULL);
pthread_mutex_lock(&f_lock);
pthread_mutex_unlock(&f_lock);
pthread_mutex_destroy(&f_lock);
// wake image download thread
pthread_mutex_lock(&mutex_download_img);
img_path = strdup(url);
pthread_cond_signal (&cond_download_img);
pthread_mutex_unlock(&mutex_download_img);
pthread_mutex_lock (&mutex_download_img);
while(!img_path) {
pthread_cond_wait(&cond_download_img, &mutex_download_img);
}
free (img_path); img_path = NULL;
pthread_mutex_unlock (&mutex_download_img);
实例
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
static pthread_cond_t cond_download_img = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t mutex_download_img = PTHREAD_MUTEX_INITIALIZER;
void thread_signal(char s[])
{
printf("This is a pthread signal.\n");
printf("%s\n",s);
sleep(1);
printf("%s\n",s);
sleep(2);
printf("signal lock here~~222222222~\n");
pthread_mutex_lock(&mutex_download_img);
printf("start to send signal here~3333333~\n");
pthread_cond_signal (&cond_download_img);
printf("cond signal and wait 5 here4444444\n");
sleep(5);
pthread_mutex_unlock(&mutex_download_img);
printf("unlock ok in signal here~5555555~\n");
pthread_exit((void*)"the first return!");
}
void thread_receive(char s[])
{
int *a = malloc(4666);
printf("This is a pthread receive .\n");
printf("%s\n",s);
*a=3;
printf("receive lock here~~\n");
pthread_mutex_lock(&mutex_download_img);
printf("pthread cond wait here in receive thread1111111111\n");
//解锁mutex_download_img等待信号,接收到信号后,上锁尝试
pthread_cond_wait(&cond_download_img, &mutex_download_img);
printf("go ahead and sleep 3 sec here6666666666\n");
sleep(3);
pthread_mutex_unlock (&mutex_download_img);
printf("thread recevie unlock here~~77777\n");
return;
}
int main(void)
{
pthread_t id1,id2;
void *a1,*a2;
int i,ret;
char *s1;
char s2[]="This is receive thread!";
s1 = (char*)malloc(100);
strcpy(s1, "This is signal thread!");
ret=pthread_create(&id1,NULL,(void *) thread_signal,s1);
if(ret!=0){
printf ("Create pthread1 error!\n");
exit (1);
}
//printf("id1 is %d\n", id1);
ret=pthread_create(&id2,NULL,(void *) thread_receive,s2);
strcpy(s1, "change here");
//free(s1);
if(ret!=0){
printf ("Create pthread2 error!\n");
exit (1);
}
#if 0
//sleep(2);//mutex lock error
sleep(4);
printf("cancel thread 1 here~~\n");
pthread_cancel(id1);//bad method because have mutex in thread
ret = pthread_mutex_trylock(&mutex_download_img);
if (0 == ret) {
//the lock is not used ,try lock have lock here
printf(" lock by try lock here\n");
} else {
//锁正在被使用;
printf("using lock here\n");
sleep(1);
pthread_mutex_unlock (&mutex_download_img);//bad use method
//pthread_cancel(id2);
}
#else
pthread_join(id1,&a1);
printf("thread 1 return value:%s\n",(char*)a1);
sleep(3);
pthread_mutex_lock(&mutex_download_img);
printf("lock in main thread here\n");
sleep(1);
pthread_mutex_unlock (&mutex_download_img);
#endif
printf("This is the main process.\n");
pthread_join(id2, NULL);
return (0);
}