Main Page | Report this Page
Linux Forum Index  »  Linux Development - System  »  thread synchronization...
Page 1 of 1    

thread synchronization...

Author Message
guru...
Posted: Wed Oct 14, 2009 10:56 pm
Guest
Hi All,

I am facing some problem with thread synchroniation. In below code
memory is allocated to buffer during call to Init(); in the same
function thread is created for the function consumeBuffer which
immediately tries to use the buffer, without checking whether buffer
is filled with data or not. my question is
What changes we need to make consumerBuffer to wait buffer to be
filled by main thread?.

I got one solution: create buffer and write to it and then create
thread which uses it.

Any other solution for this problem?


Struct mydata {
…..
BUFFER *InputBuff;

...
...
};

Init()
{
Allocate memory to inputBuff; ---(1)
Create thread(consumeBuffer) ---(2)
}


consumeBuffer (void *data)
{
Read data from (struct mydata*)data->InputBuff;
---(3)
}

Main()
{
Init();
Read data to InputBuff; ---(4)
}

Thanks
Gururaja
 
David Schwartz...
Posted: Thu Oct 15, 2009 12:11 am
Guest
On Oct 15, 1:56 am, guru <guru.nav... at (no spam) gmail.com> wrote:

Quote:
I am facing some problem with thread synchroniation. In below code
memory is allocated to buffer during call to Init(); in the same
function thread is created for the function consumeBuffer which
immediately tries to use the buffer, without checking whether buffer
is filled with data or not. my question is
What changes we need to make consumerBuffer to wait buffer to be
filled by main thread?.

Don't tell a thread to do something until you there is something for
it to do.

DS
 
Juergen Beisert...
Posted: Thu Oct 15, 2009 3:28 am
Guest
guru wrote:

Quote:
Hi All,

I am facing some problem with thread synchroniation. In below code
memory is allocated to buffer during call to Init(); in the same
function thread is created for the function consumeBuffer which
immediately tries to use the buffer, without checking whether buffer
is filled with data or not. my question is
What changes we need to make consumerBuffer to wait buffer to be
filled by main thread?.

I got one solution: create buffer and write to it and then create
thread which uses it.

Any other solution for this problem?

'man pthread_barrier*', 'man pthread_cond*', 'man pthread_mutex*', 'man
pthread_rwlock*' and 'man pthread_spin*'. I'm sure one of these locking
mechanism will solve your problem. ;-)

jbe
 
Rounak Kakkar...
Posted: Sat Oct 17, 2009 11:54 pm
Guest
On Oct 15, 3:11 pm, David Schwartz <dav... at (no spam) webmaster.com> wrote:
Quote:
On Oct 15, 1:56 am, guru <guru.nav... at (no spam) gmail.com> wrote:

I am facing some problem with thread synchroniation. In below code
memory is allocated to buffer during call to Init(); in the same
function thread is created for the function consumeBuffer which
immediately tries to use the buffer, without checking whether buffer
is filled with data or not. my question is
What changes we need to make consumerBuffer to wait buffer to be
filled by main thread?.

Don't tell a thread to do something until you there is something for
it to do.

DS

You can use a named semaphore to synchronize between two threads.
In main thread create the semaphore before creating the consume
thread.
In consume thread wait on the semaphore, using sem_wait().
In main thread after writing the data to the buffer, use sem_post to
wake up the consume thread.

Also you can use sigwait() and sigqueue().

HTH,
Rounak.
 
guru...
Posted: Mon Oct 26, 2009 2:06 am
Guest
On Oct 18, 2:54 pm, Rounak Kakkar <rounakhawk... at (no spam) gmail.com> wrote:
Quote:
On Oct 15, 3:11 pm, David Schwartz <dav... at (no spam) webmaster.com> wrote:

On Oct 15, 1:56 am, guru <guru.nav... at (no spam) gmail.com> wrote:

I am facing some problem with thread synchroniation. In below code
memory is allocated to buffer during call to Init(); in the same
function thread is created for the function consumeBuffer which
immediately tries to use the buffer, without checking whether buffer
is filled with data or not. my question is
What changes we need to make consumerBuffer to wait buffer to be
filled by main thread?.

Don't tell a thread to do something until you there is something for
it to do.

DS

You can use a named semaphore to synchronize between two threads.
In main thread create the semaphore before creating the consume
thread.
In consume thread wait on the semaphore, using sem_wait().
In main thread after writing the data to the buffer, use sem_post to
wake up the consume thread.

Also you can use sigwait() and sigqueue().

HTH,
Rounak.

Thanks Rounak.

So when I create semaphore and create thread, it will not wait for
consume thread to finish right? it will start immediately executing
next statement. when we use join function , creator thread wait for
consume thread to finish.

Regards
Gururaja
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Sun Dec 06, 2009 11:50 pm