【发布时间】:2021-10-30 05:42:07
【问题描述】:
我试图在 C++ 中创建多线程代理检查器,当我启动线程并锁定它时,所有线程都等到请求完成。我试图卸下锁,但这也无济于事。我使用 cpr 库来提出请求,可以在此处找到文档:https://whoshuu.github.io/cpr/advanced-usage.html。
可重现的例子:
#include <stdio.h>
#include <pthread.h>
#include <iostream>
#include <queue>
#include <mutex>
#include <cpr/cpr.h>
#include <fmt/format.h>
#define NUMT 10
using namespace std;
using namespace fmt;
std::mutex mut;
std::queue<std::string> q;
void* Checker(void* arg) {
while (!q.empty()) {
mut.lock();
//get a webhook at https://webhook.site
string protocol = "socks4";
string proxyformatted = format("{0}://{1}", protocol, q.front());
auto r = cpr::Get(cpr::Url{ "<webhook url>" },
cpr::Proxies{ {"http", proxyformatted}, {"https", proxyformatted} });
q.pop();
mut.unlock();
}
return NULL;
}
int main(int argc, char** argv) {
q.push("138.201.134.206:5678");
q.push("185.113.7.87:5678");
q.push("5.9.16.126:5678");
q.push("88.146.196.181:4153");
pthread_t tid[NUMT]; int i;
int thread_args[NUMT];
for (i = 0; i < NUMT; i++) {
thread_args[i] = i;
pthread_create(&tid[i], NULL, Checker, (void*) &thread_args);
}
for (i = 0; i < NUMT; i++) {
pthread_join(tid[i], NULL);
fprintf(stderr, "Thread %d terminated\n", i);
}
return 0;
}
提前致谢。
【问题讨论】:
-
有趣的是你混合了
std::mutex和 pthreads。你没有使用std::thread是有原因的吗? -
没试过
std::thread,我试试谢谢。 -
从多个线程对对象的非只读、非原子、非同步访问的未定义行为。
-
使用
std::thread不会改变任何事情。即使我不知道您的问题是什么,我也可以对此充满信心。您需要清楚地说明出了什么问题。 -
想想你想要做什么。如果互斥锁在整个事务中保持锁定状态,则多个线程毫无价值。一次只能有一个线程对队列进行操作。考虑在工作时从队列中弹出一个作业,解锁互斥锁,处理项目,然后锁定互斥锁以在作业失败时放回项目。这将允许多个线程同时工作。\
标签: c++ multithreading queue pthreads