LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 01-10-2006, 08:19 AM   #1
stephenwalter
Member
 
Registered: Jul 2005
Location: india,coimbatore
Distribution: Fedora core 4
Posts: 85

Rep: Reputation: 15
How to communicate between two threads


Hello all,
Iam working on a multithreaded application , and my requirement is that i must communicate between two threads. The flow is like this

Myfunction()
{

....
send data from here into a queue or something and just continue without waiting for the thread to process the data

}

thread_function()
{
while(1)
{
if(ptr)
{
receive the data from the queue and do something
if no data received simply continue and wait for data
/*do something*/
}
}

}

Pretty simple i have designed my logic but i find it very difficult to achieive it iam not able to find a queuing mechanism or any other mechanism for that matter to bring about this functionality
Any help in this regard will be really great.

Regards,
S.Suresh Stephen
 
Old 01-10-2006, 08:35 AM   #2
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
Requirement? Is this homework perhaps? To quote the LQ Rules:
Quote:
Do not expect LQ members to do your homework - you will learn much more by doing it yourself.
That said, if this is not homework, what language are you using (that pesudo-code is pretty generic)?
Also, try putting your code between [ code] ... [ /code] blocks so it is displayed correctly (of course, remove the spaces in the markup).
 
Old 01-10-2006, 08:43 AM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Ok so the queue is available to both threads therefore it needs to be thread safe. I assume you don't know what this means otherwise you wouldn't have asked such a question. I'm afraid you have a bit of background reading to do, but I will just give you a quick overview.

When data can be changed <edit>or viewed</edit> by more than one thread, you need to implement mutex's, what this does is ensure only one thread can access or change the data. Without them there is no guarantee that the data will be safe and may well be corrupt by the two threads.;(
This could also be done with semaphores.

If using c++ you could make your own template queue (of the std::queue) and include a mutex inside the class to hide the locking and unlocking to ensure safe data.

If using c you could create your own queue structure and once again implement the mutex inside it.

Another thing to look at is condition variables, with this your thread which is getting the msgs out the queue could could be awoken when there is a msg which has been placed in the queue, But before sending this thread back to sleep waiting for a wakeup call it should check that there are no msg's in the queue already. Otherwise a wakeup may have been sent when the thread it was intended for was already awake and working it would then go to sleep, but there would be a msg waiting to be processed. This will not be seen until another msg was pushed into the queue.

Ive tried to keep this short, but in reality it is a large subject. heres a link you should check out.
http://www.yolinux.com/TUTORIALS/Lin...ixThreads.html

Last edited by dmail; 01-10-2006 at 08:56 AM.
 
Old 01-10-2006, 11:36 PM   #4
stephenwalter
Member
 
Registered: Jul 2005
Location: india,coimbatore
Distribution: Fedora core 4
Posts: 85

Original Poster
Rep: Reputation: 15
Hello scuzzman,
Sorry i did not mean to give my work to some one , i was just trying to be a little more descriptive of my issue and i never will go against the rules of LQ. Ok now coming back to ur question , iam using c language and am working on a GUI based tool and am using gtk2.0 for the GUI. What i want to know is that u have something like a postmessage() in windows by which you can post messages to the thread which runs the and return without waiting for the main thread to process the data . Thats it

Regards,
S.Suresh Stephen
 
Old 01-13-2006, 06:52 AM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
What i want to know is that u have something like a postmessage() in windows by which you can post messages to the thread which runs the and return without waiting for the main thread to process the data . Thats it
Don't know.
 
Old 05-17-2006, 02:09 AM   #6
parikshatdubey
LQ Newbie
 
Registered: May 2006
Posts: 1

Rep: Reputation: 0
Lightbulb Re:How to communicate between two threads.

Hi Stephen!

you are using message mechanism for interprocess comunication.Stephen
you can achieve what you gave said.Just do the following as mentioned
below:

func1()
{
Create Message Queue.
Send to Message Queue.
}

func2()
{
Create Message Queue
Recieve From Message Queue.
}

Create Message Queue in both functions func1 and func2 should return same message queue id.For that purpose you need to take key and msgflag in mssget same in both these functions.

I hope this will work fine.

thanksandregards
parikshatdubey
 
Old 05-17-2006, 03:35 AM   #7
stephenwalter
Member
 
Registered: Jul 2005
Location: india,coimbatore
Distribution: Fedora core 4
Posts: 85

Original Poster
Rep: Reputation: 15
Hi ,

Thanks for your reply , but anyway since my application is a GTK based application .I have been able to implement message queues using GAsyncQueue and this just works fine . But later i had found that u can also use g_idle_add() to push data to the main GTK loop , thought that might help people who look into this thread. Thanks anyway

Regards,
S.Suresh Stephen.
 
  


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
"Find all threads started by user" not showing all threads Nylex LQ Suggestions & Feedback 3 12-28-2005 08:28 PM
0 reply threads and new threads aneroid LQ Suggestions & Feedback 7 12-18-2005 06:20 AM
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
Java Threads vs Native Threads rjmendez Programming 0 08-16-2004 05:58 AM
communicate via serial... how-to? adam_boz Linux - Newbie 4 10-22-2002 10:30 AM

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

All times are GMT -5. The time now is 04:03 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