ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
For a school assignment i'm trying to write a program that creates 50 threads; each thread puts their ID in every slot of a custom, 10000 slot vector-class and then checks to see if it is consistent. This is my code:
Code:
#include "Vector.hpp"
#include <stdio.h>
#include <pthread.h>
void *writer(void *ptr);
Vector johndoe;
int main()
{
int j, k;
pthread_t test[50];
for (j=0 ; j<50 ; j++)
{
pthread_create(test[j], NULL, writer, (void*) &j);
}
for (k=0 ; k<50 ; k++)
{
pthread_join(test[k], NULL);
}
}
void *writer(void *ptr)
{
int _id = *(int*) ptr;
if (!johndoe.setAndTest(_id))
{
printf("Thread #%d: Vector is inconsistent!!\n", _id);
}
else if((johndoe.setAndTest(_id)))
{
printf("Thread #%d: Vector is consistent\n", _id);
}
}
But this is my output:
Code:
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #24: Vector is inconsistent!!
Thread #35: Vector is inconsistent!!
Thread #35: Vector is inconsistent!!
Thread #35: Vector is inconsistent!!
Thread #35: Vector is inconsistent!!
Thread #35: Vector is inconsistent!!
Thread #35: Vector is inconsistent!!
Thread #35: Vector is inconsistent!!
Thread #35: Vector is inconsistent!!
Thread #35: Vector is inconsistent!!
Thread #35: Vector is inconsistent!!
Thread #50: Vector is inconsistent!!
Thread #50: Vector is inconsistent!!
Thread #50: Vector is inconsistent!!
Thread #50: Vector is inconsistent!!
Thread #50: Vector is inconsistent!!
Thread #50: Vector is inconsistent!!
Thread #50: Vector is inconsistent!!
Thread #50: Vector is inconsistent!!
Thread #50: Vector is inconsistent!!
Thread #50: Vector is inconsistent!!
Thread #50: Vector is inconsistent!!
Thread #35: Vector is inconsistent!!
Thread #50: Vector is inconsistent!!
Thread #50: Vector is inconsistent!!
Thread #50: Vector is inconsistent!!
Thread #50: Vector is inconsistent!!
Could you also post your custom Vector code..
What's interesting is that what should be covering code if/else seems to be being skipped (there is no consistent/not consistent for 23 for ex.)
Is your Vector code using setjmp or something?
I realised the problem was with sending a pointer to the "j"-variable used in the for-loop. It keeps increasing while the threads are running, so naturally it simply ends up at 50 and prints that out for the rest!
i solved it by declaring an int-array containing the nr. from 1 to 50 and sending those instead.
another problem i have now is, i have a 1-second sleep in the "writer"-function, but it seems to be ingnored?
I cordially suggest that the person you should be discussing this with is your teacher, so that he or she can gauge the confusion of both you and all of the other students in your class. (Otherwise, how is the teacher supposed to know?)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.