小编yas*_*shC的帖子

当等待相同的条件变量时,我们可以使用两个不同的互斥锁吗?

考虑以下场景:

线程1

mutexLk1_
gcondVar_.wait(mutexLk1);
Run Code Online (Sandbox Code Playgroud)

线程2

mutexLk2_
gcondVar_.wait(mutexLk2);
Run Code Online (Sandbox Code Playgroud)

线程3

condVar_
gcondVar_.notify_all();
Run Code Online (Sandbox Code Playgroud)

我观察到,并notify_all()没有唤醒两个线程,而是只唤醒两个线程中的一个。如果我要替换mutexLk2mutexLk1. 我得到了一个功能代码。

要重现该问题,请考虑以下来自cppref的修改示例

#include <iostream>
#include <condition_variable>
#include <thread>
#include <chrono>

std::condition_variable cv;
std::mutex cv_m1;
std::mutex cv_m; // This mutex is used for three purposes:
                 // 1) to synchronize accesses to i
                 // 2) to synchronize accesses to std::cerr
                 // 3) for the condition variable cv
int i = 0;

void waits1()
{
    std::unique_lock<std::mutex> lk(cv_m);
    std::cerr << "Waiting... \n";
    cv.wait(lk, []{return …
Run Code Online (Sandbox Code Playgroud)

c++ mutex c++17

6
推荐指数
1
解决办法
1149
查看次数

以下调用在C中意味着什么?

我正在探索sdk,并且遇到了以下语句。

uint32_t init_time = 0;
init_time = get_current_time_in_ms();
(void)init_time; // What does this statement do?
Run Code Online (Sandbox Code Playgroud)

我的第一个想法是,这是某种形式的NULL检查,但我在测试C代码中尝试执行零值和非零值,但操作类似,但两种情况下的响应都相同。

任何帮助,将不胜感激。

c cortex-m

4
推荐指数
1
解决办法
83
查看次数

虽然遇到NULL字符后C中的循环不会中断

这是添加字母数字字符串中的数字的代码:

#include<stdio.h>
#include<stdlib.h>
int main()
{
int total=0;
char ch;
printf("enter the string\n");
ch=getchar();
while(ch!='\0')
{
    printf("I am here !!");
    if (!(isalpha(ch)))
        total+=(int)ch;
    ch=(char)getchar();
    printf("I am here !!");
}
printf("\ntotal is %d",total);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

无论我输入什么字符,它都会为每个字符提供4个"我在这里".

我试着用

while((ch=getchar())!='\0');
Run Code Online (Sandbox Code Playgroud)

但它给出了同样的问题.

c while-loop

1
推荐指数
1
解决办法
671
查看次数

标签 统计

c ×2

c++ ×1

c++17 ×1

cortex-m ×1

mutex ×1

while-loop ×1