【问题标题】:How can this code be improved? I can not find any idea [closed]如何改进此代码?我找不到任何想法[关闭]
【发布时间】:2022-06-15 00:26:30
【问题描述】:

我被要求使用线程在 C++ 中编写代码,其中我必须有两个限制和 4 个字符。角色从对面接近时可以同时进入限制区域。此外,球的前后移动会影响过程,当球在区域内时,没有字母可以进来,但是,如果字母已经在它仍然可以移动的区域。 我的代码有效,但我得到了这样的评论:“您需要改进 cv.wait() 的使用以及代码中由互斥锁保护的关键区域的定义。”

#include <thread>
#include <cstring>
#include <unistd.h>
#include <chrono>
#include <ncurses.h>
#include <mutex>
#include <condition_variable>
//Task description:
//First stage: in the first stage I had to have 4 letters which go back and forth with different speeds.
//Second stage: For the second stage I had to have a critical section in which there can be only one letter from each side
//If characters approach from one side the stop, but a character from the other side can proceed. Process occurs independently.
//Third task:
//5th ball flying around back and forth and when the ball will be in the critical area, no ball can enter until that ball leaves while all the other rules are kept,
// if the others are inside they can still move.

using namespace std;
bool ready = true;
mutex m;
mutex m1;
std::condition_variable  cv;
int A=19,B=19,C=0,D=0,O;


void ball(int &point, int speed)
{
    bool incrementing = true;
    while(ready)
    {
        if(incrementing)
        {
            point ++;
            this_thread::sleep_for(chrono::milliseconds(speed));
        }

        if(point == 20 ||  point == 0)// Length of the line is 20
            incrementing = !incrementing;

        if(!incrementing)
        {
            point --;
            this_thread::sleep_for(chrono::milliseconds(speed));
        }
        if((point < 5 || point > 15)){
        cv.notify_all();
        this_thread::sleep_for(chrono::milliseconds(speed));
        }
        }


    }




void worker(int &point, int speed)
{
    bool incrementing = true;
    while(ready)
    {
        if(incrementing)
        {
            point ++;
            this_thread::sleep_for(chrono::milliseconds(speed));
        }

        if(point == 20 ||  point == 0)// Length of the line is 20
            incrementing = !incrementing;

        if(!incrementing)
        {
            point --;
            this_thread::sleep_for(chrono::milliseconds(speed));
        }


        if(point > 5 && point < 15 && incrementing){
        unique_lock<mutex> lock (m);
        cv.wait(lock);
        while(point > 5 && point < 15){
        point ++;
        this_thread::sleep_for(chrono::milliseconds(speed));
        }
        }


        if(point > 5 && point < 15 && !incrementing){
        unique_lock<mutex> locki (m1);
        cv.wait(locki);
        while(point > 5 && point < 15){
        point --;
        this_thread::sleep_for(chrono::milliseconds(speed));
        }
        }
    }
}







void Printing()
{
    while(ready)
    {
        this_thread::sleep_for(chrono::milliseconds(20));
        erase();

        mvaddch(0,A,'A');
        mvaddch(1,B,'B');
        mvaddch(2,C,'C');
        mvaddch(3,D,'D');
        mvaddch(4,O,'O');
        refresh();
        mvprintw(5,0,"------|-------|------");
        //for(int i = 0; i <= 3; i++){
        //mvprintw(i,6,"|    |");

        refresh();
        }


}



int main()
{
    initscr();
    thread thread1{[]{worker(A,500);}};
    thread thread2{[]{worker(B,500);}};
    thread thread3{[]{worker(C,500);}};
    thread thread4{[]{worker(D,500);}};
    thread threadball{[]{ball(O,300);}};
    thread thread5= thread(&Printing);

    if(getch())
    {
        ready = false;
        thread1.join();
        thread2.join();
        thread3.join();
        thread4.join();
        threadball.join();
        thread5.join();
    }
    endwin();
    return 0;
}


【问题讨论】:

  • 对于代码审查,您可以在code review site提出问题
  • 请在codereview.stackexchange.com询问改进已经工作的代码!!
  • 但是不要只是发布这个:给它一个描述它的功能的标题,在帖子中提供详细信息,并修复格式!

标签: c++ multithreading mutex conditional-variable


猜你喜欢
  • 2017-01-25
  • 1970-01-01
  • 2012-05-20
  • 1970-01-01
  • 1970-01-01
  • 2014-06-28
  • 1970-01-01
  • 2015-09-16
  • 2016-12-20
相关资源
最近更新 更多