Programming Tips - How can I do thread's on Linux?

Date: 2015may31 Language: C/C++ OS: Linux Q. How can I do thread's on Linux? A. Use POSIX threads. Here is the simplest example.
#include <pthread.h> #include <unistd.h> // For sleep() void *myThread(void *) { sleep(5000); // Replace this with what you want to do pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t threadHandle; pthread_create(&threadHandle, NULL, myThread, NULL); sleep(10000); // Delay ending the program }
Compile with:
-lpthread
An alternative is C++ standard threads. http://www.cplusplus.com/reference/thread/thread/