+ 2
How do i use for while continuing
Hi.I have build a code.It's using for loop and some codes.When for is in use,how can i continue other codes?While for loop is active?
5 Answers
+ 6
research multi-threading
https://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm
+ 4
the link i provided have an example for that but i will paste it here:
#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 () {
// threads id array with 5 slots
pthread_t threads[NUM_THREADS];
int rc;
int i;
// loop to create new threads (5)
for( i=0; i < NUM_THREADS; i++ ){
cout << "main() : creating thread, " << i << endl;
// actual thread created by calling pthread_create
// Print_Hello is the function in which the thread will work (each thread have its own function)
rc = pthread_create(&threads[i], NULL, PrintHello, (void *)i);
if (rc){
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
pthread_exit(NULL);
}
so what happens here?
we created an array of phtread
we now use a for loop to start each one by using pthread_create and passing each thread id pointer from the array, NULL value is for attributes (optional, that why it was passed as NULL), the start routine is the third parameter and we used PrintHello.
the last parameter is additional arguments to passed to the start routine and what was passed is simply the i value
this is a rather heavy subject and i suggest to research it
once you get the hang of it, it is a very powerful tool
good luck :)
+ 1
but i have a getch(); after for loop.I want getch() don't stop my codes
+ 1
burey can you give me a example code?