小编Ame*_*Jah的帖子

stl vector并发读取线程安全吗?

我正在开发一个应用程序,其中大量的线程需要迭代一组字符串值,并尝试将其自己的数据与列表中的可用数据进行匹配.

我正在寻找以下用例:

  1. Vector初始化为几个std :: string类型的元素.(可以说对象名是strList).strList将在应用程序启动时初始化.
  2. 所有线程都将遍历strList以查看它的值是否与strList的至少一个元素匹配.
  3. 没有线程会尝试修改strList,它将严格用作只读对象.

那么请告诉我并发读取是否对矢量对象是线程安全的.我使用的是RHEL 6,gcc版本是4.5.x

c++ concurrency stl thread-safety language-lawyer

23
推荐指数
2
解决办法
2万
查看次数

Boost Thread - 如何确认中断

我有阻塞任务,将由find_the_question()函数执行.但是,我不希望线程执行此函数需要超过10秒.因此,如果它需要超过10秒,我想关闭该线程清理所有资源.

我尝试为此编写代码,但不知何故,如果线程占用时间超过10秒,我无法在find_the_question()函数中获得中断.你能告诉我我做错了什么吗?

void find_the_question(std::string value)
{
    //allocate x resources
    try{
        //do some process on resources
            sleep(14);
        //clean resources
    }
    catch(boost::thread_interrupted const& )
    {
        //clean resources
        std::cout << "Worker thread interrupted" << std::endl;
    }
}

int main()
{
        boost::posix_time::time_duration timeout = boost::posix_time::milliseconds(10000);
        std::cout << "In main" << std::endl;
        boost::thread t1(find_the_question, "Can you block me");

        t1.interrupt();
        if (t1.timed_join(timeout))
        {
          //finished
          std::cout << "Worker thread finished" << std::endl;
        }
        else
        {
          //Not finished;
          std::cout << "Worker thread not finished" << std::endl;
        }

        std::cout …
Run Code Online (Sandbox Code Playgroud)

c++ interrupt boost-thread

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

如何在保存之前告诉vim自动缩进

我最近开始将vim用于我的研究生水平项目.主要问题,我有时会检查非缩进代码.我觉得如果我能以某种方式使自动缩进+保存+关闭的快捷方式然后这应该解决我的问题.

我的.vimrc文件:

set expandtab
set tabstop=2
set shiftwidth=2
set softtabstop=2
set pastetoggle=<F2>
syntax on
filetype indent plugin on
Run Code Online (Sandbox Code Playgroud)

有没有办法创建这样的命令快捷方式和覆盖:x(保存+退出).

请告诉我.

vim auto-indent

8
推荐指数
2
解决办法
5127
查看次数

使用 poll 在 C 中进行套接字连接超时

我有一个现有的多线程应用程序,它使用阻塞 connect() 调用。

但是,我想为应用程序引入连接超时,如果服务器在 x 毫秒内没有响应我们的查询,应用程序将停止尝试并给出错误。

但是,我无法弄清楚如何使用 poll 来做到这一点。

@caf 使用 select 的非阻塞连接有很大帮助。但我读到 select 比 poll 慢,因此我想使用 poll。你能告诉我这是真的吗?

我将他的代码从帖子粘贴到这里

int main(int argc, char **argv) {
u_short port;                /* user specified port number */
char *addr;                  /* will be a pointer to the address */
struct sockaddr_in address;  /* the libc network address data structure */
short int sock = -1;         /* file descriptor for the network socket */
fd_set fdset;
struct timeval tv;

if (argc != 3) …
Run Code Online (Sandbox Code Playgroud)

c sockets asynchronous rhel

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

在设计类时,应该在Constructor和Init函数中进行什么

在设计类时,我总是对构建器中放入什么以及在Init中放入什么感到困惑.那么请你就此发表看法.请在下面找到我的疑问.

  1. 类应该有单独的Init函数吗?
  2. 如果是,那么该类的构造函数和类的Init函数应该包含哪些内容.

根据我的理解,我通常倾向于总是有Init函数(对于一些懒惰的初始化).例如,如果我必须设计ConnectionPool类,那么我会这样.

class ConnectionPool
{
    string _host;
    bool _isInit;   //For separate Init, we need this

    public:
    ConnectionPool(string host)
        :_host(host),
         _isInit(false)
    {}
    void Init();
    bool isInit(){return _isInit;}
}

//This will actually make connections
ConnectionPool::Init()
{
    //Create few connections
    //if successful
    _isInit=true;
}
int main()
{
    ConnectionPool cp("host");

    //Few other init & wait for request
    //Now I have the request
    if(cp.isInit() == false)
        cp.Init();
}
Run Code Online (Sandbox Code Playgroud)

但最近,我不能拒绝具有单独的Init功能.那么请你告诉我什么是好的做法.

c++ oop

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