Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何创建和同步将请求通道发送到共享数据服务器以发送和检索请求的p_线程?_C++_Multithreading_Synchronization_Pthreads_Posix - Fatal编程技术网

C++ 如何创建和同步将请求通道发送到共享数据服务器以发送和检索请求的p_线程?

C++ 如何创建和同步将请求通道发送到共享数据服务器以发送和检索请求的p_线程?,c++,multithreading,synchronization,pthreads,posix,C++,Multithreading,Synchronization,Pthreads,Posix,我们正在课堂上学习使用posix线程。我们的任务是为多个请求通道创建一个有界缓冲区,以提取请求并将其发送到一个数据服务器 下面是类RequestChannel(为我们提供)如何发送请求: string RequestChannel::send_request(string _request) { cwrite(_request); string s = cread(); return s; } string RequestChannel::cread() { char buf[

我们正在课堂上学习使用posix线程。我们的任务是为多个请求通道创建一个有界缓冲区,以提取请求并将其发送到一个数据服务器

下面是类RequestChannel(为我们提供)如何发送请求:

string RequestChannel::send_request(string _request) {
  cwrite(_request);
  string s = cread();
  return s;
}

string RequestChannel::cread() {
  char buf[MAX_MESSAGE];
  string s = buf;
  return s;
}
string reply = chan.send_request("newthread");
我们主要制作一个叫chan的频道

RequestChannel chan("control", RequestChannel::CLIENT_SIDE);
称为“控制”,这是它发送请求的方式:

string RequestChannel::send_request(string _request) {
  cwrite(_request);
  string s = cread();
  return s;
}

string RequestChannel::cread() {
  char buf[MAX_MESSAGE];
  string s = buf;
  return s;
}
string reply = chan.send_request("newthread");
我们从一个control RequestChannel开始,它要求数据服务器为我们提供更多的RequestChannel

下面是DataServer.C的外观(也提供给我们)

(我在这里省略了处理其他请求,因为我现在只对创建一个新线程感兴趣)

这是我迷路的部分:

void process_newthread(RequestChannel & _channel, const string & _request) {
  int error;
  nthreads ++;

  // -- Name new data channel

  string new_channel_name = "data" + int2string(nthreads) + "_";
  //  cout << "new channel name = " << new_channel_name << endl;

  // -- Pass new channel name back to client

  _channel.cwrite(new_channel_name);

  // -- Construct new data channel (pointer to be passed to thread function)

  RequestChannel * data_channel = new RequestChannel(new_channel_name, RequestChannel::SERVER_SIDE);

  // -- Create new thread to handle request channel

  pthread_t thread_id;
    cout << "starting new thread " << nthreads << endl;
  if (error = pthread_create(& thread_id, NULL, handle_data_requests, data_channel)) {
    fprintf(stderr, "p_create failed: %s\n", strerror(error));
  }  

}
我自己尝试过的解决方案:

  • 在for循环中请求新线程之前和之后使用信号量。这会导致永远暂停,从不提出请求,或收到[#]的请求

    2.首先创建工作线程,在工作线程内部请求一个新的请求通道线程-->这通常会导致尝试创建第一个线程的错误,并永久暂停或所有工作线程争先恐后地发出相同的请求,在这种情况下,我使用了一个与所有工作线程共享的信号量,但它又回到了永远暂停第一个线程的状态。在这种情况下,我弄乱了信号量,但我要么得到一个或两个这样的错误

  • 对于工作线程,传入一个包含指向控制线程(和其他缓冲区)指针的结构,并在该函数中请求新的请求通道。与#2相同的错误

  • 几次尝试cout's,只做一个线程来测试它停止的位置

  • 我认为我缺少对线程的基本理解

    *我知道复制别人的代码是不道德的,但我想看看在我自己尝试之后正确的解决方案是什么样子。但它不起作用,我的解决方案也不起作用。我想了解它是如何工作的,并在将来制定自己的解决方案


    • (制作我自己的信号量和boundedbuffer)
    如果您还需要什么,请询问。这是我第一次在stackoverflow上发帖

    :: ./simpleclient
    CLIENT STARTED:
    Establishing control channel... done.
    
    
    making boundedbufferslist
    Creating channels for worker threads...
    Received request : newthread
    Request Channel (control) writing [newthread]in cread(), s is...uest
    Request Channel (control) reads [uest]
    processed cwrite : uest
    Reading next request from channel (control) ...in cread(), s is...newthread
    Request Channel (control) reads [newthread]
     done (control).
    New request is newthread
    ^C