I passed a structure into pthread_create. One component of the structure is a vector data. the "data" push_back in a loop in each thread. When the size of the loop is small, the code runs correctly. When the loop is large. I got the following error message:
munmap_chunk(): invalid pointer
munmap_chunk(): invalid pointer
Aborted (core dumped)
I tried m<100, it works. When trying the m<1000, it shows the error.
// compile using: g++ parallel_2.C -o oo -lpthread
#include <iostream>
#include <cstdlib>
#include <vector>
#include <thread>
using namespace std;
const unsigned NUM_THREADS = std::thread::hardware_concurrency();
//
struct INPUT
{
int start;
int end;
vector<int> data;
};
//
void *Loop(void *param)
{
INPUT *in = (INPUT*)param;
int start = in->start;
int end = in->end;
cout<<" start: "<<start<<" end: "<<end<<endl;
//for(int m=0; m<100000000; m++)
for(int i = start;i < end;i++)
for(int m=0; m<1000; m++) {
in->data.push_back(i);
}
//pthread_exit(NULL);
}
//
int main ()
{
pthread_t threads[NUM_THREADS];
INPUT input[NUM_THREADS];
for( int i=0; i < NUM_THREADS; i++ ){
cout << "main() : creating thread, " << i << endl;
input[i].start = i*5;
input[i].end = input[i].start + 5;
int rc = pthread_create(&threads[i], NULL,
Loop, (void *)&input[i]);
if (rc){
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
for(int i = 0; i<NUM_THREADS; i++)
cout<<"!! size of "<<i<<": "<<input[0].data.size()<<endl;
pthread_exit(NULL);
}
munmap_chunk(): invalid pointer
munmap_chunk(): invalid pointer
Aborted (core dumped)
In the specific case of this example (main() assumes that the threads are done and consults the modified structures), you have to join() a thread before accessing the structure it is modifying.
for(int i = 0; i<NUM_THREADS; i++)
{
pthread_join(threads[i], NULL);
cout<<"!! size of "<<i<<": "<<input[0].data.size()<<endl;
}
This way, you are certain it is done, and not modifying the structure any more.
The problem did not show up with very few iterations because the threads had probably (but nothing is certain) ended their task before your last loop in main() tried to access their structures.
By the way, you should consider using std::thread.
(https://en.cppreference.com/w/cpp/thread/thread/thread)
Related
I am observing strange behavior using pthreads. Note the following code -
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <pthread.h>
#include <unistd.h>
typedef struct _FOO_{
int ii=0;
std::string x="DEFAULT";
}foo;
void *dump(void *x)
{
foo *X;
X = (foo *)x;
std::cout << X->x << std::endl;
X->ii+=1;
}
int main(int argc, char **argv)
{
foo X[2];
const char *U[2] = {"Hello", "World"};
pthread_t t_id[2];
int t_status[2];
/*initalize data structures*/
for(int ii=0; ii < 2; ii+=1){
X[ii].x=U[ii];
}
foo *p = X;
for(int ii=0; ii < 2; ii+=1){
t_status[ii] = pthread_create(&t_id[ii], NULL, dump, (void *)p);
std::cout << "Thread ID = " << t_id[ii] << " Status = " << t_status[ii] << std::endl;
p+=1;
}
//sleep(1); /*if this is left commented out, one of the threads do not execute*/
for(int ii=0; ii < 2; ii+=1){
std::cout << pthread_join(t_status[ii], NULL) << std::endl;
}
for(int ii=0; ii < 2; ii+=1){
std::cout << X[ii].ii << std::endl;
}
}
When I leave the sleep(1) (between thread create and join) call commented out, I get erratic behavior in the randomly only 1 of the 2 thread run.
rajatkmitra#butterfly:~/mpi/tmp$ ./foo
Thread ID = 139646898239232 Status = 0
Hello
Thread ID = 139646889846528 Status = 0
3
3
1
0
When I uncomment sleep(1). Both threads execute reliably.
rajatkmitra#butterfly:~/mpi/tmp$ ./foo
Thread ID = 140072074356480 Status = 0
Hello
Thread ID = 140072065963776 Status = 0
World
3
3
1
1
The pthread_join() should hold up exit from the program, till both threads complete, but in this example I am unable to get that to happen without the sleep() function. I really do not like the implementation with sleep(). Can someone tell me if I am missing something??
See Peter's note -
pthread_join should be called with the thread id, not the status value that pthread_create returned. So: pthread_join(t_id[ii], NULL), not pthread_join(t_status[ii], NULL). Even better, since the question is tagged C++, use std::thread. –
Pete Becker
I'm working on a college assignment and have been tasked with showing a basic mutex lock example. I've never worked with threads in any form, so I'm a total beginner working with POSIX threads in C++.
What I'm trying to get the program to do is create 1000 threads that increment a global integer by 1000.
#include <iostream>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <thread>
pthread_t threadArr[1000];
pthread_mutex_t lock;
// Global int to increment
int numberToInc = 0;
void* incByTwo(void*)
{
pthread_mutex_lock(&lock);
for(int j = 0; j < 1000; j++){
numberToInc += 1;
}
pthread_mutex_unlock(&lock);
return NULL;
}
int main()
{
//Creates 1000 threads with incByTwo func
for(int i = 0; i < 1000; i++){
pthread_create(&threadArr[i], NULL, incByTwo, NULL);
}
std::cout << "\n" << numberToInc << "\n";
return 0;
}
The following produces a series of different results, obviously because the threads are executing concurrently, right?
Now, I've gotten it to work correctly by inserting
for(int i = 0; i < 1000; i++){
pthread_join(threadArr[i], NULL);
}
After the thread creation loop, but then removing the mutex locks, it still works. I've been trying to piece out how pthread_join works but I'm a little lost. Any advice?
Sorted a way to show the mutex lock in action. So when I output the global var in the function, without mutex locks it has the potential to show the results out of order.
Running the number range with mutex locks, out looks like:
1000
2000
3000
... (etc)
10000
With mutex locks removed, the output can vary in the order.
E.g.
1000
2000
4000
6000
3000
5000
7000
8000
9000
10000
While the final result of the three threads is correct, the sequence is out of order. In the context of this program it doesn't really matter but I'd imagine if it's passing inconsistently sequenced values it messes things up?
pthread_t threadArr[10];
pthread_mutex_t lock;
int numberToInc = 0;
void* incByTwo(void*)
{
pthread_mutex_lock(&lock);
for(int j = 0; j < 1000; j++){
numberToInc += 1;
}
std::cout << numberToInc << "\n";
pthread_mutex_unlock(&lock);
return NULL;
}
int main()
{
if (pthread_mutex_init(&lock, NULL) != 0)
{
printf("\n mutex init failed\n");
return 1;
}
for(int i = 0; i < 10; i++){
pthread_create(&threadArr[i], NULL, incByTwo, NULL);
}
pthread_join(threadArr[0], NULL);
return 0;
}
I am trying to send vector as data to pthread. But when I am trying to print the thread id , its coming garbage value.
If I run this code with single thread, it works fine. But when I run it with 2 threads, its not working.
#include <iostream>
#include <pthread.h>
#include <vector>
using namespace std;
struct val {
int data;
int sData;
};
void *foo(void *a)
{
vector <val>* b = (vector <val>*)a;
for (val it : *b) {
std::cout <<" thread " <<it.data;
std::cout <<" &&& " <<it.sData<<"-----------"<<endl;
}
}
int main()
{
pthread_t thr[2];
for (int j = 0; j < 2; j++) {
std::vector <val> *a = new std::vector<val>(10);
for (int i = 0; i< 10; i++) {
val t;
t.data = j;
t.sData = j*10;
a->push_back(t);
}
pthread_create(&thr[j], NULL, &foo, &a);
}
pthread_join(thr[0],NULL);
pthread_join(thr[1],NULL);
return 0;
}
Expected Output:
thread 0 &&& 0
....
....
thread 1 &&& 10
thread 1 &&& 10
....
....
You are giving the thread a pointer to a local variable. That variable is destroyed immediately afterwards, at the closing brace of the loop. foo ends up accessing a dangling pointer, whereupon your program exhibits undefined behavior.
I've got simple c++ code that firstly creates 4 threads and then sends 1000 times number 1 to the c++ queue followed by four 0. Each of these threads tries to read from that queue and when any thread reads 0, it terminates and prints its local sum. If it reads 1 then it simply adds 1 to the sum. The reading is protected with mutex. Code works as intended in 4 out of 5 times, but sometimes I get double free or corruption (!prev) error... ...Abort core dumped. I've also debugged the code with gdb but only got received signal SIGABRT, Aborted ... ...at raise.c: No such file or directory". I'm not explicitly allocating or deallocating any memory. What could be causing the problem?
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
std::queue<int> my_queue;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *process(void *arg){
int sum = 0;
while(1){
if(!my_queue.empty()){
pthread_mutex_lock(&mutex);
if (my_queue.front() == 1){
sum += 1;
my_queue.pop();
pthread_mutex_unlock(&mutex);
}
else{
my_queue.pop();
printf("Sum: %d\n", sum);
pthread_mutex_unlock(&mutex);
break;
}
}
}
return arg;
}
int main(void){
pthread_t id[4];
for(int i = 0; i < 4; i++){
if (pthread_create(&id[i], NULL, process, NULL) != 0){
fprintf(stderr, "%s\n", "Error creating thread!");
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < 1000; i++){
my_queue.push(1);
}
for (int i = 0; i < 4; i++){
my_queue.push(0);
}
for (int i = 0; i < 4; i++){
pthread_join(id[i], NULL);
}
return EXIT_SUCCESS;
}
You have two problems:
Firstly you are pushing onto the queue without holding the lock, secondly you are testing if the queue is empty without holding the lock.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
std::queue<int> my_queue;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *process(void *arg){
int sum = 0;
bool keep_going = true;
while(keep_going){
pthread_mutex_lock(&mutex);
if(!my_queue.empty()){
if (my_queue.front() == 1){
sum += 1;
}
else{
keep_going=false;
}
}
my_queue.pop();
pthread_mutex_unlock(&mutex);
}
printf("Sum: %d\n", sum); // Don't do IO while holding a lock!
return arg;
}
int main(void){
pthread_t id[4];
// Initialize queue *before* creating threads.
for (int i = 0; i < 1000; i++){
my_queue.push(1);
}
for (int i = 0; i < 4; i++){
my_queue.push(0);
}
// Create threads
for(int i = 0; i < 4; i++){
if (pthread_create(&id[i], NULL, process, NULL) != 0){
fprintf(stderr, "%s\n", "Error creating thread!");
exit(EXIT_FAILURE);
}
}
// Join them.
for (int i = 0; i < 4; i++){
pthread_join(id[i], NULL);
}
return EXIT_SUCCESS;
}
If you want to add to the queue after creating the threads, you need something like:
pthread_mutex_lock(&mutex);
my_queue.push(value);
pthread_mutex_unlock(&mutex);
Inside the for-loops. Also, there is then a real chance of the queue emptying before you get to the zeros. Either do it properly by waiting on semaphores, or the queue loop needs to becomes something like:
pthread_mutex_lock(&mutex);
if(my_queue.empty()){
pthread_mutex_unlock(&mutex);
usleep(1);
pthread_mutex_lock(&mutex);
} else {
Where the thread will briefly sleep to let the queue fill up.
Also, you are writing in C++. Learn to write a RAII class to call pthread_mutex_lock and unlock.
This is my first time here. I really hope anyone can help me out there. So this is my problem. I keep getting run time error #2 something about a corrupt "arr". But the program runs fine until the end. I can't figure it out.
This is my code:
#include <iostream>
using namespace std;
void main(){
int arr1[3];
int temp;
//INPUT NUMBERS
for (int i=0; i<5;i++)
{
cin>>arr1[i];
}
cout<<endl;
//SORT
for(int c=0;c<5;c++)
{
for (int k=0;k<5;k++)
{
if(arr1[c]<arr1[k])
{
temp=arr1[k];
arr1[k]=arr1[c];
arr1[c]=temp;
}
}
}
for (int m=0; m<5; m++)
{
cout<<arr1[m]<<endl;
}
}
Try this out:
#include <iostream>
using namespace std;
int main()
{
int arr1[5];
int temp;
//INPUT NUMBERS
for (int i = 0; i < 5; i++) {
cin >> arr1[i];
}
cout << endl;
//SORT
for (int c = 0; c < 5; c++) {
for (int k = 0; k < 5; k++) {
if (arr1[c] < arr1[k]) {
temp = arr1[k];
arr1[k] = arr1[c];
arr1[c] = temp;
}
}
}
for (int m = 0; m < 5; m++) {
cout << arr1[m] << endl;
}
}
It compiles properly without any errors. The mistake you had made is in declaring the size of the array. If you want to store 5 in puts, you need to declare an array of size 5. Your code might work, but a good compiler will always give out an error.
The reason being that when you declare an array, you actually create a pointer to the first element of the array. And then, some memory regions are kept for this array, depending on the size. If you try to access an element that is outside these memory regions, you may encounter a garbage value.
Here's your code in ideone.