C 中的多线程程序:在标志变量上使用Mutex
Multithread program in C++: Using mutex on a flag variable
我当前正在处理C 中的多线程代码,在该代码中,将每个线程设置为执行一块代码,除非全局标志变为 true 。除全局标志外,线程不会共享任何数据。我想知道最好的做法是否是为国旗添加一个哑光。
为了使这个问题保持简单,我将求助于以下琐碎的示例:假设您正在尝试查找大型向量是否包含给定的整数,并且您想在几个线程之间将搜索分开。有一个全局变量,其初始值为 false 。如果其中一个线程找到了整数,则应将标志设置为 true ,并且所有线程都应在之后停止(如果某些线程执行一些额外的操作是可以的,直到他们意识到标志已更改为止(。
以下代码执行此操作(对于COUT的比赛条件,我深表歉意,但我想简短代码(。它创建了一个填充500的大型矢量,用100替换了500个,并实现了搜索过程。
代码编译并似乎有效,但是我想知道在某个时候,标志的读/写入是否会出现问题。我想知道是否应该为标志变量添加一个哑光。我很犹豫,因为(1(在大多数情况下,线程只会读取标志的值,(2(标志只会更改一次(它永远不会更改为false(,(3(标志的值为不要更改线程执行中的数据(除了停止它们之外,除了停止它们之外别无其他操作((4(如果线程继续进行一些迭代,则可以。
我也许应该只为函子中的写作部分添加一个mutex锁(flag = true(?
vector <int> a;
bool flag = false;
int size = 100000000
class Fctor {
int s;
int t;
public:
Fctor(int s, int t) : s(s), t(t) {}
\ finds if there is a 100 in the vector between positions s and t
void operator()() {
int i = 0;
for (i = s; i < t; i++) {
if (flag == true) break;
if (a[i] == 100) {
flag = true;
break;
}
}
cout << s << " " << t << " " << flag << " " << i << "n";
}
};
int main() {
a = vector<int> (8 * size, 500); \creates a vector filled with 500
a[2*size+1] = 100; // This position will have a 100
chrono::high_resolution_clock::time_point begin_time =
chrono::high_resolution_clock::now();
int cores = 4;
vector<std::thread> threads;
int num = 8/cores;
int s = 0;
int t = num * size;
Fctor fctor(s, t);
std::thread th(fctor);
threads.push_back(std::move(th));
for (int i = 1; i < cores; i++) {
int s1 = i * num * size+1;
int t1 = (i+1) * num * size;
Fctor fctor1(s1, t1);
std::thread th1(fctor1);
threads.push_back(std::move(th1));
}
for (std::thread& th : threads) {
th.join();
}
chrono::high_resolution_clock::time_point end_time =
chrono::high_resolution_clock::now();
chrono::duration<double> time_span =
chrono::duration_cast<chrono::duration<double> >(end_time -
begin_time);
cout << "Found in: "<< time_span.count() << " seconds. n";
return 0;
}
将标志设置为原子
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- enum是C++中的宏变量还是整数变量
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 用C++中的一个变量定义一个常量
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 你能重载对象变量名本身返回的内容吗
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 尝试通过多个向量访问变量时,向量下标超出范围
- 试图让变量检查数组中的某些内容
- Cpp-Tuple使用带有变量的get
- 将包含C样式数组的对象初始化为成员变量(C++)
- std::mutex作为一个成员变量对多个线程来说是安全的吗
- 有什么理由C++ 11+ std::mutex 应该声明为全局变量,而不是作为函数参数传递到 std::thread 中
- C 中的多线程程序:在标志变量上使用Mutex
- POSIX线程 - (MUTEX和条件变量)问题
- C 98 Mutex中共享数组的锁定变量