Need the PID of a process in a thread

How do I run a process from a C++ p_thread and obtain its PID? Right now I am doing in a p_thread:

system("ampl method.run");

and into the main function (after running the thread mentioned before):

sleep(5); //Just to be sure that the thread is executing the ampl command
system("ps -Ac| grep ampl | awk '{print $1}' > ~/AMPL_PID.txt");

This doesn't work. The main program hangs in the "system" function while the thread continues to execute. Once I manually kill the ampl process in the thread, the main program continues and writes the system("ps -Ac...") output to the desired location.

I need to be able to obtain the ampl command PID in order to later kill it (if necessary). Any ideas how to make this work or how to do this?

Two things - pids work on process, threadid on individual threads. Linux has threads running as lite processes. So this answer only works where every thread is really a separate process.

pid_t pid=getpid();

Write this value to a file - from inside the thread function.