多线程题目

来源:互联网 发布:视频图像分析算法 编辑:程序博客网 时间:2024/06/03 09:43

1、基本概念

        详见:线程和进程关系和区别、同步和互斥、进程间通信

2、以下多线程对int型变量x的操作,哪几个不需要进行同步(D)
        A. x=y;      B. x++;    C. ++x;    D. x=1;

        详见:多线程二 多线程中的隐蔽问题揭秘

3、多线程中栈与堆是公有的还是私有的 (C)

        A:栈公有, 堆私有

        B:栈公有,堆公有

        C:栈私有, 堆公有

        D:栈私有,堆私有

4、临界区(Critical Section)和互斥量(Mutex)

        两者都可以用于同一进程中不同子线程对资源的互斥访问。

        互斥量是内核对象,因此还可以用于不同进程中子线程对资源的互斥访问。

        互斥量可以很好的解决由于线程意外终止资源无法释放的问题。

5、一个全局变量tally,两个线程并发执行(代码段都是ThreadProc),问两个线程都结束后,tally取值范围。

        inttally = 0;//glable

        voidThreadProc()

        {

              for(inti = 1;i <= 50;i++)

                   tally += 1;

        }

        答:[50,100]

6、编写一个程序,开启3个线程,这3个线程的ID分别为ABC,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC.依次递推。

#include <iostream>  #include <pthread.h>  #include<unistd.h>#include<stdio.h>#include <sys/time.h>#include <time.h>using namespace std;    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  static int order_mark = 0;#define MAX_LOOP 10void *thread_func(void *arg)  {int idx = *(int *) arg;printf("child thread starts, idx: %d\n", idx);while (1) {    pthread_mutex_lock(&mutex);    while(order_mark % 3 != idx)      {          pthread_cond_wait(&cond, &mutex);      }        if (order_mark >= (3*MAX_LOOP)) {    order_mark++;    pthread_mutex_unlock(&mutex);    pthread_cond_broadcast(&cond);        break;}    printf("thread %d: %lu\n", idx, pthread_self());    order_mark++;    pthread_mutex_unlock(&mutex);pthread_cond_broadcast(&cond);}return NULL;}int main(int argc, char **argv){pthread_t tids[3];int idx[3], i;for (i = 0; i < 3; i++) {idx[i] = i;pthread_create(&tids[i], NULL, thread_func, &idx[i]);}for (i = 0; i < 3; i++) {pthread_join(tids[i], NULL);printf("thread %d exits\n", i);}printf("finish\n");pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);return 0;}


7、生产者消费者问题:有一个生产者在生产产品,这些产品将提供给若干个消费者去消费,为了使生产者和消费者能并发执行,在两者之间设置一个有多个缓冲区的缓冲池,生产者将它生产的产品放入一个缓冲区中,消费者可以从缓冲区中取走产品进行消费,所有生产者和消费者都是异步方式运行的,但它们必须保持同步,即不允许消费者到一个空的缓冲区中取产品,也不允许生产者向一个已经装满产品且尚未被取走的缓冲区中投放产品。


8、读者写者问题:这也是一个非常经典的多线程题目,题目大意如下:有一个写者很多读者,多个读者可以同时读文件,但写者在写文件时不允许有读者在读文件,同样有读者读时写者也不能写。

    分析:首先来找找哪些是属于“等待”情况。

第一、写者要等到没有读者时才能去写文件。

第二、所有读者要等待写者完成写文件后才能去读文件。

找完“等待”情况后,再看看有没有要互斥访问的资源。由于只有一个写者而读者们是可以共享的读文件,所以按题目要求并没有需要互斥访问的资源。


9、有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空。现要让四个文件呈如下格式:

A:1 2 3 4 1 2....

B:2 3 4 1 2 3....

C:3 4 1 2 3 4....

D:4 1 2 3 4 1....

请设计程序。

         参考第6题,还没想好?


10、 子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,主线程循环100次,依次交替循环 50次

#include <iostream>  #include <pthread.h>  #include<unistd.h>#include<stdio.h>#include <sys/time.h>#include <time.h>using namespace std;    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_t tid;  // flag=0, sub thread to run, or the main thread when flag=1static int flag = 0;static int cnt = 0;#define LOOP_MAX 10time_t tms, tme;void *thread_func(void *arg)  {  tme = time(NULL);printf("child thread starts, time consume: %d\n", tme - tms);while (1) {    pthread_mutex_lock(&mutex);    while(flag == 1)      {          pthread_cond_wait(&cond, &mutex);      }        // print    tme = time(NULL);    printf("================ No %d, time: %d ==================\n", cnt, tme - tms);    for (int i = 0; i < 5; i++) {    printf("child thread: %d\n", i);}cnt++;flag = 1;if (cnt == LOOP_MAX) {pthread_mutex_unlock(&mutex);pthread_cond_broadcast(&cond);break;}    pthread_mutex_unlock(&mutex);pthread_cond_broadcast(&cond);}printf("child thread exit\n");return NULL;}int main(int argc, char **argv){tms = time(NULL);pthread_create(&tid, NULL, thread_func, NULL);while (1) {    pthread_mutex_lock(&mutex);    while(flag == 0)      {          pthread_cond_wait(&cond, &mutex);      }        // print    for (int i = 0; i < 10; i++) {    printf("main thread: %d\n", i);}flag = 0;if (cnt == LOOP_MAX) {pthread_mutex_unlock(&mutex);break;}    pthread_mutex_unlock(&mutex);pthread_cond_broadcast(&cond);}printf("main thread finish\n");pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);return 0;}

11. 有一int型全局变量g_Flag初始值为0;在主线程中启动线程1,打印“this is thread1”,并将g_Flag设置为1, 在主线程中启动线程2,打印“this is thread2”,并将g_Flag设置为2 线程序1需要在线程2退出后才能退出主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出。


Linux的多线程的一些面试题

1.控制输出数据

http://blog.csdn.net/chencheng126/article/details/44488603


2.主线程和子线程分别循环一定的次数

http://blog.csdn.net/chencheng126/article/details/44592003


3.生产者和消费者

http://blog.csdn.net/chencheng126/article/details/44593105


4.经典面试题的解答

http://blog.csdn.net/chencheng126/article/details/44652533

http://blog.csdn.net/chencheng126/article/details/44652607



0 0