Thread Synchronization

there is a problem in my result

Page 1 of 1

3 Replies - 1074 Views - Last Post: 05 October 2009 - 11:54 AM Rate Topic: -----

#1 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Thread Synchronization

Posted 03 October 2009 - 02:02 PM

Hi, here's my main code for a program:
int main() {
		cout<<"File to split: ";
		string fileN;
		getline(cin, I1.fname);
		I1.partsize = "10m";
		retval = pthread_create(&thKfs, NULL, runDivine, (void*)&I1);
		cout<<endl;
		while(percentage != 100.0) {
				usleep(1000);
				pthread_mutex_lock(&mutPct);
				cout<<"\rProgress: "<<percentage<<" %";
				pthread_mutex_unlock(&mutPct);
		}
		pthread_join(thKfs, NULL);
		cout<<endl;
		return 0;
}

The problem is... that I get a result like this:
File to split: /home/kian/Downloads/backup.tar.gz

Progress: 100 %47 %%%%

The progress goes up correctly... but there are extra stuff after it. How can I solve it ?

Is This A Good Question/Topic? 0
  • +

Replies To: Thread Synchronization

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Thread Synchronization

Posted 03 October 2009 - 04:11 PM

...i realize its kind of a lame solution, but I always just print an bunch of extra spaces to clear out anything that may be there form before...
Was This Post Helpful? 1
  • +
  • -

#3 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: Thread Synchronization

Posted 04 October 2009 - 01:28 AM

:D I suspected that and now it's solved. Thanks! just 10 spaces and it's clear :^:
Was This Post Helpful? 0
  • +
  • -

#4 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: Thread Synchronization

Posted 05 October 2009 - 11:54 AM

Sorry to post again, but it seems there's another problem here:
int main() {
		cout<<"File to split: ";
		string fileN;
		getline(cin, I1.fname);
		I1.partsize = "10m";
		retval = pthread_create(&thKfs, NULL, runDivine, (void*)&I1);
		cout<<endl;
		int floored=0, c=0;
		while(percentage != 100.0) {
				cout<<"\r";
				usleep(5000); // *** Note ***
				pthread_mutex_lock(&mutPct);
				floored = floor(percentage);
				pthread_mutex_unlock(&mutPct);
				cout<<"Progress: "<<floored<<" %";
						if(c==0) {
								cout<<" -   ";
								c++;
						} else if(c==1) {
								cout<<" \\   ";
								c++;
						} else if(c==2) {
								cout<<" |   ";
								c++;
						} else if(c==3) {
								cout<<" /   ";
								c = 0;
						}
		}
		pthread_join(thKfs, NULL);
		cout<<endl;
		return 0;
} 

in the usleep function, when I change the number to 1000000 (1 second) something odd happens: the progress display won't show up at all, then when the splitting is over, it just writes: "Progress: 100 % -";
But with lower numbers, like the 5000 as in the code above, it works correct.
I guess it's because with this number, there is a conflict with the variable lock part... which means when we want to use it, it's locked... is that correct ?

This post has been edited by Anarion: 06 October 2009 - 10:41 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1