关于线程同步例程中的一个问题,大家看看
有一个线程同步的例子,程序是对的,这里只贴出看不明白的部分。
1. 开始的typedef void* (*fun)(void*);有什么用,这句话应该如何理解?
2. 使用的时候,func前后加了2个下划线,为什么?
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<errno.h>
#include<unistd.h>
typedef void* (*fun)(void*); //** 这有什么用?如何理解?
int g_Flag=0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void* thread1(void*);
void* thread2(void*);
/*
* when program is started, a single thread is created, called the initial thread or main thread.
* Additional threads are created by pthread_create.
* So we just need to create two thread in main().
*/
int main(int argc, char** argv)
{
printf("enter main\n");
pthread_t tid1, tid2;
int rc1=0, rc2=0;
rc2 = pthread_create(&tid2, NULL, thread2, NULL);
if(rc2 != 0)
printf("%s: %d\n",__func__, strerror(rc2)); //** 这里为什么__func__,前后都是2个下划线
rc1 = pthread_create(&tid1, NULL, thread1, &tid2);
if(rc1 != 0)
printf("%s: %d\n",__func__, strerror(rc1));
pthread_cond_wait(&cond, &mutex);
printf("leave main\n");
exit(0);
}