Dave's Brain

Browse - 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/

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.