LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2012, 04:46 AM   #1
Rebold
LQ Newbie
 
Registered: Feb 2012
Location: Randers, Denmark
Distribution: Kubuntu
Posts: 3

Rep: Reputation: Disabled
Question Posix Threads


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!!
What am i doing wrong??

Hope you can help
 
Old 03-14-2012, 05:58 AM   #2
adixon
Member
 
Registered: Oct 2010
Posts: 34

Rep: Reputation: 3
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?

regards, Alex
 
Old 03-14-2012, 06:26 AM   #3
Rebold
LQ Newbie
 
Registered: Feb 2012
Location: Randers, Denmark
Distribution: Kubuntu
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hey Alex!

Thanks for your reply!

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?
 
Old 03-14-2012, 09:00 AM   #4
adixon
Member
 
Registered: Oct 2010
Posts: 34

Rep: Reputation: 3
Where are you calling the sleep func, could you post updated writer function..
 
Old 03-14-2012, 09:10 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,657
Blog Entries: 4

Rep: Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938Reputation: 3938
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?)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Synchronize 2 posix threads vbx_wx Programming 4 03-15-2011 07:58 AM
POSIX Threads embesil Programming 4 03-07-2009 01:03 PM
POSIX Threads mannahazarika Programming 4 07-16-2006 11:00 PM
About POSIX threads Ephracis Programming 1 12-03-2004 06:33 AM
I have a problem with POSIX Threads. AndreasA Programming 2 08-11-2003 02:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration