1.

Write a program to dynamic allocate a 2D Array, assign values to the same, print the values and deallocate the 2D Array.

Answer»

Using pthread_create we create a THREAD. We can USE the reference of a structure to pass multiple arguments to the 4th parameter as below:

#include <iostream.h> #include <pthread.h> struct arg_struct {    int arg1;    int arg2; }; void *print_the_arguments(void *arguments) {    struct arg_struct *args = (struct arg_struct *)arguments;    cout<< args -> arg1<<ENDL;    cout<< args -> arg2<<endl;    pthread_exit(NULL);    RETURN NULL; } int main() {    pthread_t some_thread;    struct arg_struct args;    args.arg1 = 5;    args.arg2 = 7;    if (pthread_create(&some_thread, NULL, &print_the_arguments, (void *)&args) != 0) {        printf("Uh-oh!\n");        return -1;    }    return pthread_join(some_thread, NULL); /* Wait until thread is finished */ }    return pthread_join(some_thread, NULL); /* Wait until thread is finished */ }


Discussion

No Comment Found