1

I've got a very small program to test thread related stuff:

#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
int main()
{
    pid_t pid=getpid();
    pid_t tid=gettid();
    printf("%d,%d\n",pid,tid);
    return 0;
}

Within vim editor, I focus on 'gettid' and Shift-K, the man page of gettid says it's inside sys/types. No problem, when I compile it, there's error:

g++ mythread.cpp 
mythread.cpp: In function ‘int main()’:
mythread.cpp:7:22: error: ‘gettid’ was not declared in this scope
     pid_t tid=gettid();
                  ^

I'm on ubuntu1604 with new gcc version. How to fix it?

Hind Forsum
  • 9,717
  • 13
  • 63
  • 119
  • Possible duplicate of [C++ gettid() was not declared in this scope](http://stackoverflow.com/questions/30680550/c-gettid-was-not-declared-in-this-scope) – ks1322 Jan 09 '17 at 15:05

1 Answers1

4

use : pid_t tid = syscall(SYS_gettid); as this cant be called directly.

General Foch
  • 333
  • 1
  • 7