我正在使用 C 和 pthreads 库开发多线程客户端,使用 boss/worker 架构设计,并且在理解/调试范围后堆栈使用错误时遇到问题,该错误导致我的客户端失败。(我对C有点陌生)
我尝试了多种方法,包括全局定义变量、传递双指针引用等。
Boss logic within main:
for (i = 0; i < nrequests; i++)
{
struct Request_work_item *request_ctx = malloc(sizeof(*request_ctx));
request_ctx->server = server;
request_ctx->port = port;
request_ctx->nrequests = nrequests;
req_path = get_path(); //Gets a file path to work on
request_ctx->path = req_path;
steque_item work_item = &request_ctx; // steque_item is a void* so passing it a pointer to the Request_work_item
pthread_mutex_lock(&mutex);
while (steque_isempty(&work_queue) == 0) //Wait for the queue to be empty to add more …Run Code Online (Sandbox Code Playgroud) 我在将值存储到 void* 并成功检索我最初存储的内容时遇到问题。以下是我的伪代码/思路:
客户端内部方法 1
StatusCode doSomething() {
string filename = "somefile.txt";
void* server_checksum;
//Stat signature (string &filename, void* file_status)
StatusCode fileStatus = Stat(filename, &server_checksum); //Passing the address of the pointer
//We received the fileStatus from Stat, I expect the value of server_checksum to match what the server sent
//However, this prints a completely different number, and I do not know how to ensure it holds the right value
cout << *((uint32_t *)serverCrc) << endl;
return StatusCode::OK;
}
Run Code Online (Sandbox Code Playgroud)
在客户端的 Stat …