Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- How to multi-thread in C++:
- Multithreading is a specialized form of multitasking and a multitasking is the feature that allows your computer to run two or more programs concurrently. In general, there are two types of multitasking: process-based and thread-based.
- Process-based multitasking handles the concurrent execution of programs. Thread-based multitasking deals with the concurrent execution of pieces of the same program.
- A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.
- C++ does not contain any built-in support for multithreaded applications. Instead, it relies entirely upon the operating system to provide this feature.
- This tutorial assumes that you are working on Linux OS and we are going to write multi-threaded C++ program using POSIX. POSIX Threads, or Pthreads provides API which are available on many Unix-like POSIX systems such as FreeBSD, NetBSD, GNU/Linux, Mac OS X and Solaris.
- */
- #include <iostream>
- #include <cstdlib>
- #include <pthread.h>
- using namespace std;
- #define NUM_THREADS 5
- void *PrintHello(void *threadid)
- {
- long tid;
- tid = (long)threadid;
- cout << "Hello World! Thread ID, " << tid << endl;
- pthread_exit(NULL);
- }
- int main ()
- {
- pthread_t threads[NUM_THREADS];
- int rc;
- int i;
- for( i=0; i < NUM_THREADS; i++ ){
- cout << "main() : creating thread, " << i << endl;
- rc = pthread_create(&threads[i], NULL,
- PrintHello, (void *)i);
- if (rc){
- cout << "Error:unable to create thread," << rc << endl;
- exit(-1);
- }
- }
- pthread_exit(NULL);
- }
- //Source: http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm
- //BaSs_HaXoR
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement