简体   繁体   English

用C语言进行多线程和O3编译

[英]Multithreading and O3 compilation in C

I'm writing code that tries to detect when i signal changes from 0 to 1 as fast as possible (real time application). 我正在编写代码,尝试检测何时我尽可能快地将信号从0更改为1(实时应用程序)。 I have the following two functions 我有以下两个功能

void *SensorSignalReader (void *arg)
{

  char buffer[30];
  struct timeval tv;
  time_t curtime;

  srand(time(NULL));

  while (1) {
    int t = rand() % 10 + 1; // wait up to 1 sec in 10ths
    usleep(t*100000);

    int r = rand() % N;
    signalArray[r] ^= 1;

    if (signalArray[r]) {
      changedSignal = r;
      gettimeofday(&tv, NULL);
      timeStamp[r] = tv;
      curtime = tv.tv_sec;
      strftime(buffer,30,"%d-%m-%Y  %T.",localtime(&curtime));
      printf("Changed %5d at Time %s%ld\n",r,buffer,tv.tv_usec);
    }
  }
}

void *ChangeDetector (void *arg)
{
  char buffer[30];
  struct timeval tv;
  time_t curtime;
  int index;

  while (1) {

    while (changedSignal == -1) {} // issues with O3
    gettimeofday(&tv, NULL);
    index = changedSignal;
    changedSignal = -1;

    curtime = tv.tv_sec;
    if(timeStamp[index].tv_usec>tv.tv_usec){
      tv.tv_usec += 1000000;
      tv.tv_sec--;
    }
    strftime(buffer,30,"%d-%m-%Y  %T.",localtime(&curtime));
    printf("Detcted %5d at Time %s%ld after %ld.%06ld sec\n---\n",index,buffer,tv.tv_usec,
    tv.tv_sec - timeStamp[index].tv_sec,
    tv.tv_usec - timeStamp[index].tv_usec);
  }
}

I have 2 pthreads running at all times, one for each function. 我一直在运行2个pthread,每个函数一个。 When i compile normally ( gcc -lpthread ) This works as intended. 当我正常编译时( gcc -lpthread )这按预期工作。 SensorSignalReader changes changedSignal and ChangeDetector detects it as the while loop breaks. SensorSignalReader改变changedSignalChangeDetector检测为while循环中断。 When I compile with the -O3 or flag though it feels like the variable changedSignal never changes? 当我使用-O3或flag进行编译时,虽然感觉变量changedSignal从未改变? The while loop in ChangeDetector runs forever while signals are being changed constantly. 当信号不断变化时, ChangeDetector的while循环将永远运行。 If I put a printf("%d\\n",changedSignal); 如果我放置一个printf("%d\\n",changedSignal); inside there, it prints -1 all the time. 在里面,它一直打印-1。 There is something done by O3 that I do not understand. O3有一些我不了解的事情。 What is it? 它是什么?

It's very likely your program is experiencing undefined behaviour and you just got lucky when you didn't have optimisations switched on. 您的程序很有可能遇到未定义的行为,并且您没有打开优化就很幸运。

changedSignal appears to be a shared resource so you need to use atomic operations or some form of locking to ensure that threads won't simultaneously access it. changedSignal似乎是共享资源,因此您需要使用原子操作或某种形式的锁定,以确保线程不会同时访问它。

You can use the pthread functions for locking or gcc 's builtin functions for atomic operations . 您可以使用pthread函数进行锁定,也可以使用gcc内置函数进行原子操作

Edit: As pointed out by Olaf, it looks like you're trying to implement a producer-consumer pattern. 编辑:正如Olaf指出的那样,您似乎正在尝试实现生产者-消费者模式。 You might want to try implementing this by using condition variables instead of trying to reinvent it. 您可能想尝试通过使用条件变量来实现这一目标,而不是尝试重新发明它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM