linux 下的多线程小例子

多线程面试题:
是否熟悉POSIX多线程编程技术?如熟悉,编写程序完成如下功能:
1)有一个int型全局变量g_Flag初始值为0;
2)在主线程中启动线程1,打印“this is thread1”,兵将g_Flag设置为1;
3) 在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2;
4) 线程序1需要在线程2退出后才能退出;
5) 主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出。

 

 

 

test.c/

#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));

 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);
}

/*
 * thread1() will be execute by thread1, after pthread_create()
 * it will set g_Flag = 1;
 */
void* thread1(void* arg)
{
 printf("enter thread1\n");
 printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
 pthread_mutex_lock(&mutex);
 if(g_Flag == 2)
  pthread_cond_signal(&cond);
 g_Flag = 1;
 printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
 pthread_mutex_unlock(&mutex);
 pthread_join(*(pthread_t*)arg, NULL);
 printf("leave thread1\n");
 pthread_exit(0);
}

/*
 * thread2() will be execute by thread2, after pthread_create()
 * it will set g_Flag = 2;
 */
void* thread2(void* arg)
{
 printf("enter thread2\n");
 printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
 pthread_mutex_lock(&mutex);
 if(g_Flag == 1)
  pthread_cond_signal(&cond);
 g_Flag = 2;
 printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
 pthread_mutex_unlock(&mutex);
 printf("leave thread2\n");
 pthread_exit(0);
}

 

编译:gcc -lpthread test.c -o test
运行:   ./test
OK 运行一下就是你要的结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值