LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-24-2012, 06:17 PM   #1
franmf
LQ Newbie
 
Registered: Oct 2012
Posts: 28

Rep: Reputation: Disabled
pthreads in C. critical section


hi people, I need the following

Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int size;
static pthread_mutex_t cs_mutex = PTHREAD_MUTEX_INITIALIZER;

void *f1(void *vargp)
{
	while(1){
		pthread_mutex_lock( &cs_mutex );
		printf("processing size\n");
		size = 0;
		sleep(10);
		size = 10;
		printf("finishing processing size\n");
		pthread_mutex_unlock( &cs_mutex );
		sleep(40);
	}
}

void *f2(void *vargp){

      while(1){

           --ANY INSTRUCCION USING SIZE

          sleep(60);
      }



}
int main(int argc, char *argv[])
{ 
	pthread_create(&tid, NULL, f1, NULL);
	pthread_create(&tid2, NULL, f2, NULL);
	pthread_join(tid,NULL);
	pthread_join(tid2,NULL);

	return 0;
}
What I need is that f2 CAN'T use size when f1 is in the "critical section" or just the same, when f1 is using or modifing size.

How can I manage to reach that?

thank you

Last edited by franmf; 10-24-2012 at 06:19 PM.
 
Old 10-24-2012, 06:19 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by franmf View Post
hi people, I need the following
Please include a question with your post!

Also, why would you place a sleep() in-between a Mutex lock() and unlock() calls????

And your second thread loops infinitely without delay. Again, why???

Edit:

Forget my second comment; you edited your post.
 
1 members found this post helpful.
Old 10-24-2012, 06:20 PM   #3
franmf
LQ Newbie
 
Registered: Oct 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
Please include a question with your post!

Also, why would you place a sleep() in-between a Mutex lock() and unlock() calls????

And your second thread loops infinitely without delay. Again, why???
sorry, I press enter and posted it unintentionally without finishing the post and you didn't give me time to edit it
 
Old 10-24-2012, 06:21 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by franmf View Post
What I need is that f2 CAN'T use size when f1 is in the "critical section" or just the same, when f1 is using or modifing size.

How can I manage to reach that?

thank you
Of course 'size' can be used within the second thread. Why would you think it cannot?
 
1 members found this post helpful.
Old 10-24-2012, 06:23 PM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
If you want to prevent f2 from using 'size' while f1 is using/modifying it, then use the mutex (in both threads) to prevent concurrent access to it.
 
1 members found this post helpful.
Old 10-24-2012, 06:23 PM   #6
franmf
LQ Newbie
 
Registered: Oct 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
Of course 'size' can be used within the second thread. Why would you think it cannot?
what I mean is that when f1() is in the critical section, and modifing size, f2() can't use it. f2() only can use it when f1() has finished the critical section.

I don't know if I'm explaining myself. Sorry for my english

---------- Post added 10-24-12 at 06:24 PM ----------

Quote:
Originally Posted by dwhitney67 View Post
If you want to prevent f2 from using 'size' while f1 is using/modifying it, then use the mutex (in both threads) to prevent concurrent access to it.
I need just that, could you tell me how to do it? thank you dwhitney67
 
Old 10-24-2012, 06:24 PM   #7
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by franmf View Post
what I mean is that when f1() is in the critical section, and modifing size, f2() can't use it. f2() only can use it when f1() has finished the critical section.

I don't know if I'm explaining myself. Sorry for my english
It took me a moment to understand you; see my follow-up post about protecting access to 'size' using the mutex.
 
1 members found this post helpful.
Old 10-24-2012, 06:29 PM   #8
franmf
LQ Newbie
 
Registered: Oct 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
could you show me how to do that dwhitney67? I've seen several examples where both pthreads use the same code, but in my case both pthreads use different codes, so I don't know how to do it exactly

Last edited by franmf; 10-24-2012 at 06:31 PM.
 
Old 10-24-2012, 06:29 PM   #9
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
For example:
Code:
#include <stdio.h>     /* for printf */
#include <unistd.h>    /* for sleep */
#include <pthread.h>
#include <stdbool.h>   /* for boolean type */

volatile int size = 0;

static pthread_mutex_t cs_mutex = PTHREAD_MUTEX_INITIALIZER;

void* f1(void* arg)
{
    while (true)
    {
        pthread_mutex_lock(&cs_mutex);
        ++size;
        pthread_mutex_unlock(&cs_mutex);

        sleep(1);
    }

    return NULL;
}

void* f2(void* arg)
{
    while (true)
    {
        pthread_mutex_lock(&cs_mutex);
        printf("size is: %d\n", size);
        pthread_mutex_unlock(&cs_mutex);

        sleep(1);
    }

    return NULL;
}

int main()
{
    pthread_t t1, t2;

    pthread_create(&t1, NULL, f1, NULL);
    pthread_create(&t2, NULL, f2, NULL);

    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    return 0;
}
 
1 members found this post helpful.
Old 10-24-2012, 06:41 PM   #10
franmf
LQ Newbie
 
Registered: Oct 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
fucking genious dwhiteney67. Tested and running!

thank you very very much, for being so fast answering and doing it properly.

I own you one. hope to see you around here.
 
  


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
Deadlock due to crash inside critical section Jerry Mcguire Programming 3 05-12-2011 08:16 AM
How to program critical section for reader-writer systems? sinu_nayak2001 Programming 2 05-21-2010 12:43 PM
any function for entering critical section? ebd Programming 12 09-03-2009 12:08 AM
any function for entering critical section? ebd Linux - Newbie 1 09-01-2009 12:54 PM
how to enter critical section in Linux Enviroment ebd Programming 2 09-01-2009 10:24 AM

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

All times are GMT -5. The time now is 12:12 PM.

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