File: function_pthread.cpp

package info (click to toggle)
android-platform-system-extras 7.0.0%2Br33-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,136 kB
  • sloc: cpp: 28,180; ansic: 14,543; python: 5,376; sh: 2,441; java: 908; asm: 299; makefile: 19; xml: 12
file content (33 lines) | stat: -rw-r--r-- 703 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

constexpr int LOOP_COUNT = 100000000;

void* ChildThreadFunction(void*) {
  for (volatile int i = 0; i < LOOP_COUNT; ++i) {
  }
  return nullptr;
}

void MainThreadFunction() {
  for (volatile int i = 0; i < LOOP_COUNT; ++i) {
  }
}

int main() {
  pthread_t thread;
  int ret = pthread_create(&thread, nullptr, ChildThreadFunction, nullptr);
  if (ret != 0) {
    fprintf(stderr, "pthread_create failed: %s\n", strerror(ret));
    exit(1);
  }
  MainThreadFunction();
  ret = pthread_join(thread, nullptr);
  if (ret != 0) {
    fprintf(stderr, "pthread_join failed: %s\n", strerror(ret));
    exit(1);
  }
  return 0;
}