小编Mur*_*los的帖子

不确定我是否需要互斥锁

我是并发编程的新手,所以要好.我有一个基本的顺序程序(用于家庭作业),我试图把它变成一个多线程程序.我不确定我的第二个共享变量是否需要锁定.线程应修改我的变量但从不读它们.应该读取的唯一时间是在生成所有线程的循环完成分发键之后.

#define ARRAYSIZE 50000

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h> 

void binary_search(int *array, int key, int min, int max); 

int count = 0; // count of intersections
int l_array[ARRAYSIZE * 2]; //array to check for intersection

int main(void)
{
    int r_array[ARRAYSIZE]; //array of keys
    int ix = 0;

    struct timeval start, stop;
    double elapsed;

    for(ix = 0; ix < ARRAYSIZE; ix++)
    {
        r_array[ix] = ix;
    }
    for(ix = 0; ix < ARRAYSIZE * 2; ix++)
    {
        l_array[ix] = …
Run Code Online (Sandbox Code Playgroud)

c concurrency mutex pthreads

4
推荐指数
2
解决办法
221
查看次数

sqrt()返回INF

嗨我正在尝试用长双打做一些计算,我从sqrt()函数得到INF.

码:

#include <math.h>
#include <stdio.h>

int main()
{
    long double bigNUMBER;
    long double p1,p2,p3;
    long double p4;

    p1 = 4.769547e+155;
    p2 = 1.012994e+170;
    p3 = 1.714812e+169;

    p4 = p1*p1 + p2*p2 + p3*p3;

    bigNUMBER = sqrt(p4);

    printf("product: %Lf\n\n", p4); // 1055562645989439942507809393771156765931135...

    printf("\n result %Lf\n\n", bigNUMBER); // INF

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

c math.h sqrt

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

摆脱带符号的零

我正在做一些矩阵运算,其中矩阵元素通过以下方式从某些变量获取值:

elem[1] = -x
Run Code Online (Sandbox Code Playgroud)

但当它在矩阵中x = 0设置时,这是不希望的。-0有什么明确的方法可以防止这种情况发生吗?

c++ floating-point

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

C++用宏替换函数

我们的C++代码中有一个现有的函数实现:

void Function(int param)
{
    printf("In Function\n");
}

int main()
{
    Function(10);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望将其更改为调用另一个函数(通过宏声明的帮助),它将接受其他参数,如 FILELINE(用于调试目的),然后调用实际函数:

#define Function(param) Function_debug(param, __FILE__,  __FUNCTION__,  __LINE__) \
{\
    printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); \
    Function(param);\
}
Run Code Online (Sandbox Code Playgroud)

但是下面的代码:

#include <stdio.h>

#define Function(param) Function_debug(param, __FILE__,  __FUNCTION__,  __LINE__) \
{\
    printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); \
    Function(param);\
}

void Function(int param)
{
    printf("In Function\n");
}

int main()
{
    Function(10);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

翻译为:

void Function_debug(int param, "temp.cpp", …
Run Code Online (Sandbox Code Playgroud)

c c++

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

将boost :: spirit :: qi :: phrase_parse()与qi :: grammar一起使用时出错

我正在做一个IRC消息解析器,Boost.Spirit但是当我尝试解析输入时,我得到一个(非常长的)错误.我遵循了" 罗马数字 "的例子.另外,我使用的是g++4.7-std=c++11.只有当我打电话时发生错误phrase_parse()test.cpp,不是我做的一个实例message_grammar.

语法类是:

class message_grammar : qi::grammar<std::string::const_iterator, std::string()>
{
public:
    message_grammar() : base_type(m_message)
    {   
        using qi::_val;
        using qi::_1;
        using boost::spirit::ascii::char_;
        using qi::lit;

        qi::rule<std::string::const_iterator, std::string()> alpha, graph, number, special, user,
            nick, chn, channel;

        alpha  = qi::as_string[qi::alpha];
        graph  = qi::as_string[qi::graph];
        number = qi::as_string[char_('0', '9')];
        chn    = qi::as_string[(char_('#') | char_('$'))];

        special = qi::as_string[
                char_('-') | char_('[') | char_(']') | char_('\\') 
            |   char_('`') | char_('^') | char_('{') | char_('}')
        ];

        user …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-spirit boost-spirit-qi c++11

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