Write a program using Multithreading in C++ while using pthread_join()?
Write a program in the shell using threads. Each thread must be doing one of the tasks mentioned below. One task must be completed and then another task should be started. Use pthread_join() system call to make the thread wait for the other thread's completion. Get the input at the start and use these numbers in all threads. The numbers must consist of four digits at least. i.e 1234. a) Finding the largest of three numbers b) Reversing the largest number c) Sum of individual digits of a 4-digit number (1234 -> 1+2+3+4=10)
do you know?
how many words do you know
See also questions close to this topic
-
C++ increment with macro
I have the following code and I want to use increment with macro:
#include <iostream> #define ABS(x) ((x) < 0 ? -(x) : (x)) int main(int argc, char** argv) { int x = 5; const int result = ABS(x++); std::cout << "R: " << result << std::endl; std::cout << "X: " << x << std::endl; return EXIT_SUCCESS; }
But output will be incorrect:
R: 6 X: 7
Is it possible to somehow use macros with an increment, or should this be abandoned altogether?
-
Can anyone pls tell me whats wrong with my code ? im stuck for the last 3 hours ,this question is bipartite graph in c++
idk why im getting error ,can someone help ? im trying to prove if a graph is bipartite or not in c++
bool isBipartite(vector<int> graph[],int V) { vector<int> vis(V,0); vector<int> color(V,-1); color[0]=1; queue <int> q; q.push(0); while (!q.empty()) { int temp = q.front(); q.pop(); for (int i=0;i<V;i++) { if (!vis[i] && color[i] == -1) "if there is an edge, and colour is not assigned" { color[i] = 1 - color[temp]; q.push(i); vis[i]=1; } else if (!vis[i] && color[i] == color[temp] "if there is an edge and both vertices have same colours" { vis[i]=1; return 0; // graph is not bipartite } } } return 1; }
it gives output "no" for whatever i enter
-
How to assign two or more values to a QMap Variable in Qt
I am getting confused of how to store the values assigned from 3 different functions and storing them in a single map variable
QMap<QString,QString> TrainMap = nullptr; if(......) ( TrainMap = PrevDayTrainMap(); TrainMap = NextDayTrainMap(); TrainMap = CurrentDayTrainMap(); }
The PrevDayTrainMap,NextDayTrainMap & CurrentDayTrainMap returns a set of values with Date and the TrainIdName.I need to store all the values from prevday,currentday and nextday in the TrainMap but it stores only the currentday values to the TrainMap as it is assigned at the last.I am not sure what to do so that it doesn't overwrite.If I should merge what is the way to do it?
-
Multithreading using springboot
I’m new to multithreading and I want to know how to approach it for my code, currently files are getting generated and processed into fielder using single thread hence unable to handle bulk file generation abs this needs to be handled using multithreading . How do I approach this ? Using java 11 and xml format springboot approach .
-
Will volatile work for Collections and objects?
Trying to understand the theory about
volatile
. They say,volatile
provides happens-before, so changes done in thread 1 should be visible in thread 2.What if we will put volatile for Collection? Will java provide visibility in Thread 2 for changes done in Thread 1 ? And why?
public class Vol { volatile List<String> list = new ArrayList<>(); // thread 1 public void put(String s) { list.add(s); } // thread 2 public List<String> get(){ // all changes are visible? "Why" for No\Yes answer return list; } }
-
cpu core threads vs process threads
i have a linux machine lscpu output says. CPU(s): 80 On-line CPU(s) list: 0-79 Thread(s) per core: 2 which means maximum 160 threads can be created.
the output of /proc/sys/kernel/threads-max 1731877
this is indicating i can create maximum of 1731877 thread can be created.
can any one help what is the difference between cpu core threads and the kernal max threads. question may basic but will be helpful for my understanding. thanks in advance .
-
How to pass the argument 3 in pthread_create if we have a array and how to become returning values?Prime numbers check
I wanted to know how to get the result like: Number x is prime or Number x is not prime, sure this threaded ex. in 10 parts. How to pass argument 3 if we have an array and how the function checks an array if is prime or not? How can I do that? Thanks in advance...I'm a beginner so sorry for any mistakes that I did in the code Code:
#include <stdio.h> #include ... //defining global variables #define ARRAYSIZE 1000000 #define MAXTHREAD 10 int arr[ARRAYSIZE]; //since we split array in parts,we need a variable to reprensent a part of array int part=0; //Function to fill the array with values void fill_array(int arr[],int size){...} //Function to check if a numer is prime or not int prime(long int num){ //return 0:not prime //return 1:prime //Function to deside,if a element of the array is prime or not(threaded) void* prime_threaded(void *vargp){ //casting a void pointer to integer int num=*((int *)vargp); int thread_part=part++; int start=thread_part*(ARRAYSIZE/MAXTHREAD); int end=(thread_part +1)*(ARRAYSIZE/MAXTHREAD); printf("Thread works in segment %d\n",part); int* returnvalue = (int*) malloc(sizeof(int)); for(int i=start;i<end;i++){ if(prime(num==0)){ *returnvalue=0; return returnvalue; }} *returnvalue=1; return returnvalue;} int main(){ fill_array(arr,ARRAYSIZE); pthread_t threads[MAXTHREAD]; for(int i=0;i<MAXTHREAD;i++){ pthread_create(&threads[i],NULL,prime_threaded,(void*)arr); } for(int i = 0; i < MAXTHREAD; i++) { void * exit_status_thread; pthread_join(threads[i], &exit_status_thread); int result = *(int *) exit_status_thread; free(exit_status_thread); if(result==0){ printf("\n the number is not prime\n "); }else{ printf("\n the number is prime\n"); } } return 0; }