繁体   English   中英

具有3个线程的多线程程序,可打印数字序列

[英]Multi-threaded program with 3 threads that prints sequence of numbers

我正在尝试编写一个利用3个线程并打印0、1、2、3、4、5,....等的程序。

线程1-打印0、3、6、9等
线程2-打印1,4,7,10等
线程3-打印2,5,8,11等

谁能帮我编写代码吗?

一种可能的解决方案涉及消息队列。 每个线程都有一个消息队列。

  • 线程1写入0 ,然后将0发送到消息队列2。
  • 线程2从消息队列2读取值,将该值递增,然后将新值发送到消息队列3。
  • 线程3从消息队列3读取值,对其进行递增,然后将新值发送到消息队列1

  • 从这里,您应该推断出下一步线程1应该做什么。

以下程序使用条件变量进行同步。 如果需要任何修改或发现任何错误,请帮助我。

#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#define THREAD_LIMIT  3
pthread_mutex_t     mutex = PTHREAD_MUTEX_INITIALIZER;
int PRINT_MAX = 500;
pthread_cond_t  cond[THREAD_LIMIT];//  = PTHREAD_COND_INITIALIZER;

struct thread_arg
{
    int index;
    pthread_cond_t *waitCond;
    pthread_cond_t *signalCond;
};

void* threadFun(void* pArg)
{
    struct thread_arg *pThreadArg = (struct thread_arg *)pArg;
    int i=pThreadArg->index, rValue;
    for(;i< PRINT_MAX;i += THREAD_LIMIT)
    {
        rValue = pthread_mutex_lock(&mutex);
        printf("thread index = %d  value = %d \n",(pThreadArg->index+1), i);

        rValue = pthread_cond_signal(pThreadArg->signalCond);
        //wait for turn
        while( pthread_cond_wait(pThreadArg->waitCond, &mutex) != 0 )
        {}
        rValue = pthread_mutex_unlock(&mutex);
    }

    rValue = pthread_cond_signal(pThreadArg->signalCond);
    return NULL;    
}


int main()
{
    pthread_t ThreadId[THREAD_LIMIT];
    struct thread_arg ThreadArg[THREAD_LIMIT];

    for (int i =0; i< THREAD_LIMIT ; i++)
        pthread_cond_init(&cond[i], NULL);

    for (int i =0; i< THREAD_LIMIT ; i++)
    {
        ThreadArg[i].index = i;
        ThreadArg[i].waitCond = &cond[i];
        if (i == THREAD_LIMIT-1)
            ThreadArg[i].signalCond = &cond[0];
        else
            ThreadArg[i].signalCond = &cond[i+1];

        printf("starting Thread %d \n",i+1);
        pthread_create(&ThreadId[i], NULL, &threadFun,(void*)&ThreadArg[i]);
        usleep(500);
    }
    for (int i =0; i< THREAD_LIMIT ; i++)
        pthread_join(ThreadId[i],NULL);

    return 0;
}

具有3个线程的多线程程序,它使用条件变量和互斥锁打印数字序列。

什么是条件变量和互斥锁以及何时使用它以及如何使用? http://thispointer.com/c11-multithreading-part-7-condition-variables-explained/

#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>
using namespace std;

//Creating 3 condition variable to perform synchronization in 3 different functions using wait and notify function on condition variable
condition_variable cv1, cv2, cv3;
//creating mutex variable to lock and unlock the resource    
mutex m1;
void function1()
{
    //creating unique_lock on the mutex so that we don't have to take care whether mutex is unlocked or not, as mutex gets unlocked as soon as unique_lock is out of scope (using concept of RAII)
    unique_lock<mutex> lock(m1);
    for(int i=0; i<10; i+=3)
    {
        cout<<i<<endl;

        //We notify function2 using notify_one function, once we are done printing values from function one and makes function1 in wait state by calling wait function
        cv2.notify_one();
        cv1.wait(lock); 
    }

}
void function2()
{   
    unique_lock<mutex> lock(m1);
    for(int i=1; i<10; i+=3)
    {       
        cout<<i<<endl;

        //We notify function3 using notify_one function, once we are done printing values from function2 and makes function2 in wait state by calling wait function
        cv3.notify_one();
        cv2.wait(lock);     
    }   
}

void function3()
{   
    unique_lock<mutex> lock(m1);
    for(int i=2; i<10; i+=3)
    {       
        cout<<i<<endl;

       //We notify function1 using notify_one function, once we are done printing values from function3 and makes function3 in wait state by calling wait function
        cv1.notify_one();
        cv3.wait(lock);     
    }   
}

int main()
{
    //creating 3 threads (t1, t2 & t3) and associating functions with each thread
    thread t1(function1);
    thread t2(function2);
    thread t3(function3);

    t1.join();
    t2.join();
    t3.join();

    return 0;
}           

OUTPUT:

0
1
2
3
4
5
6
7
8
9

这可能是一个更好的解决方案

#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>
using namespace std;

#define MAX 10

condition_variable cv1, cv2, cv3; 
mutex m1;
int i;
void function1()
{
    while (i < MAX) {
        unique_lock<mutex> lock(m1);
        cv1.wait(lock);
        cout << i << endl;
        i++;
        lock.unlock();
        cv2.notify_one();
    }

}
void function2()
{
    while (i < MAX) {
        unique_lock<mutex> lock(m1);
        cv2.wait(lock);
        cout << i << endl;
        i++;
        lock.unlock();
        cv3.notify_one();
    }
}

void function3()
{   
    while (i < MAX) {
        unique_lock<mutex> lock(m1);        
        cv3.wait(lock);
        cout << i << endl;
        i++;
        lock.unlock();
        cv1.notify_one();
    }
}

int main()
{
    thread t3(function3);
    thread t1(function1);
    thread t2(function2);

    cv1.notify_one();

    t1.join();
    t2.join();
    t3.join();

    getchar();
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM